Issue
Am working with json record below. I want to get all the text fields/values. when I try to display record,
This is what I got
{"message":"Cont050. "}{"message":"Contoso. "}{"message":"123. "}{"message":"Main. "}
{"message":"street. "}{"message":"Redmond,. "}{"message":"WA. "}{"message":"98052. "}
{"message":"987454-3210. "}{"message":"6\/10\/2019. "}{"message":"Sales. "}{"message":"Associate:. "}
{"message":"Paul. "}{"message":"1. "}{"message":"Cappuccino. "}{"message":"I. "}{"message":"BACON. "}
{"message":"&. "}{"message":"EGGS. "}{"message":"Sunny-side-up. "}{"message":"Tax. "}{"message":"Total. "}
{"message":"$220. "}{"message":"9.5. "}
Here is what I want to achieve:
what am expecting to get is to have all the text values ascribed to just a single message variable like below
{"message":"Cont050 Contoso 123 Main street Redmond, WA 98052 987454-3210 6/10/2019 Sales Associate: Paul 1 Cappuccino I BACON & EGGS Sunny-side-up Tax Total $220 9.5 "}
Below is my coding efforts so far
$spacing = " ";
$output = '
{
"language": "en",
"textAngle": 0.0,
"orientation": "Up",
"regions": [{
"boundingBox": "236,480,851,2269",
"lines": [{
"boundingBox": "646,480,441,115",
"words": [{
"boundingBox": "646,480,441,115",
"text": "Cont050"
}]
},
{
"boundingBox": "308,576,198,106",
"words": [{
"boundingBox": "308,576,198,106",
"text": "Contoso"
}]
},
{
"boundingBox": "301,683,386,137",
"words": [{
"boundingBox": "301,683,80,75",
"text": "123"
},
{
"boundingBox": "403,706,115,86",
"text": "Main"
},
{
"boundingBox": "538,744,149,76",
"text": "street"
}
]
},
{
"boundingBox": "293,792,521,125",
"words": [{
"boundingBox": "293,792,255,120",
"text": "Redmond,"
},
{
"boundingBox": "564,849,77,66",
"text": "WA"
},
{
"boundingBox": "661,859,153,58",
"text": "98052"
}
]
},
{
"boundingBox": "278,1009,367,106",
"words": [{
"boundingBox": "278,1009,367,106",
"text": "987454-3210"
}]
},
{
"boundingBox": "265,1231,257,92",
"words": [{
"boundingBox": "265,1231,257,92",
"text": "6/10/2019"
}]
},
{
"boundingBox": "259,1352,596,92",
"words": [{
"boundingBox": "259,1352,155,73",
"text": "Sales"
},
{
"boundingBox": "434,1368,275,76",
"text": "Associate:"
}, {
"boundingBox": "732,1380,123,63",
"text": "Paul"
}
]
},
{
"boundingBox": "249,1587,18,69",
"words": [{
"boundingBox": "249,1587,18,69",
"text": "1"
}]
},
{
"boundingBox": "326,1591,327,92",
"words": [{
"boundingBox": "326,1591,327,92",
"text": "Cappuccino"
}]
},
{
"boundingBox": "236,1842,498,75",
"words": [{
"boundingBox": "236,1842,19,73",
"text": "I"
},
{
"boundingBox": "315,1843,190,73",
"text": "BACON"
}, {
"boundingBox": "529,1847,39,69",
"text": "&"
},
{
"boundingBox": "593,1847,141,70",
"text": "EGGS"
}
]
}, {
"boundingBox": "350,1978,393,82",
"words": [{
"boundingBox": "350,1978,393,82",
"text": "Sunny-side-up"
}]
},
{
"boundingBox": "445,2371,113,88",
"words": [{
"boundingBox": "445,2371,113,88",
"text": "Tax"
}]
},
{
"boundingBox": "437,2644,166,105",
"words": [{
"boundingBox": "437,2644,166,105",
"text": "Total"
}]
}
]
},
{
"boundingBox": "1113,1580,153,465",
"lines": [{
"boundingBox": "1113,1580,153,84",
"words": [{
"boundingBox": "1113,1580,153,84",
"text": "$220"
}]
}, {
"boundingBox": "1139,1954,122,91",
"words": [{
"boundingBox": "1139,1954,122,91",
"text": "9.5"
}]
}]
}
],
"modelVersion": "2021-04-01"
}
';
$json = json_decode($output, true);
foreach ($json['regions'] as $row1) {
//echo $boundingBox = $row1['boundingBox'];
foreach ($row1['lines'] as $row2) {
//echo $boundingBox = $row2['boundingBox'];
foreach ($row2['words'] as $row3) {
$words = $row3['text'];
$extracts = "$words.$spacing";
$output_response = array(
"message" => $extracts);
echo json_encode($output_response);
}
}
}
Solution
You shouldn’t echo a new JSON object each time through the loop. Put the text in an array, then use implode()
at the end to combine them into a single string, and put that in a single JSON object.
$message = [];
foreach ($json['regions'] as $row1) {
//echo $boundingBox = $row1['boundingBox'];
foreach ($row1['lines'] as $row2) {
//echo $boundingBox = $row2['boundingBox'];
foreach ($row2['words'] as $row3) {
$message[] = $row3['text'];
}
}
}
echo json_encode(['message' => implode(' ', $message)]);
Answered By – Barmar
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0