json - PHP json_decode with long strings and special characters

Solution:
There is a problem with the decoded JSON you provide theprint_r
output of: the key iFlag appears twice in the same object. This should not be possible.
The original JSON string you decoded would have looked like this:
$json = '[
{
"id": 1535497,
"tid": 6970000,
"text": "Hello :) error is back! It\'s quite annoying!",
"dFlag": 1,
"iFlag": null,
"rFlag": null,
"member": null,
"contact": {
"id": 187,
"name": "User Name",
"_info": {
"contact_href": "https:\/\/url\/187"
}
},
"iFlag": null,
"cFlag": null,
"processNotifications": null,
"dateCreated": "2015-12-08T13:59:19Z",
"createdBy": "User Name [email protected] (email)",
"eFlag": 1,
"_info": {
"lastUpdated": "2015-12-08T13:59:19Z",
"updatedBy": "User Name [email protected] (email)"
}
}
]';
And indeed, if I decode and print it like this:
$data = json_decode($json, true); // translate objects to associative arrays
print_r ($data);
...the output is the same as you provided in the question, with the exception of the duplicate iFlag property:
Array ( [0] => Array ( [id] => 1535497 [tid] => 6970000 [text] => Hello :) error is back! It's quite annoying! [dFlag] => 1 [iFlag] => [rFlag] => [member] => [contact] => Array ( [id] => 187 [name] => User Name [_info] => Array ( [contact_href] => https://url/187 ) ) [cFlag] => [processNotifications] => [dateCreated] => 2015-12-08T13:59:19Z [createdBy] => User Name [email protected] (email) [eFlag] => 1 [_info] => Array ( [lastUpdated] => 2015-12-08T13:59:19Z [updatedBy] => User Name [email protected] (email) ) ) )
That the array beautifier breaks on this, just means that this beautifier is not good enough. It tries to interpret the output ofprint_r
, but apparently it has some weaknesses. This however is no indication that there is a problem with JSON encoding/decoding.
print_r
does in fact a good job in "beautifying" the output itself, but in a browser context you need to preserve the formatting to see it, e.g. by wrapping that output inpre
tags. If you do, you'll see this output:
Array
(
[0] => Array
(
[id] => 1535497
[tid] => 6970000
[text] => Hello :) error is back! It's quite annoying!
[dFlag] => 1
[iFlag] =>
[rFlag] =>
[member] =>
[contact] => Array
(
[id] => 187
[name] => User Name
[_info] => Array
(
[contact_href] => https://url/187
)
)
[cFlag] =>
[processNotifications] =>
[dateCreated] => 2015-12-08T13:59:19Z
[createdBy] => User Name [email protected] (email)
[eFlag] => 1
[_info] => Array
(
[lastUpdated] => 2015-12-08T13:59:19Z
[updatedBy] => User Name [email protected] (email)
)
)
)
Now we could JSON encode that data again:
$json = json_encode($data, JSON_PRETTY_PRINT);
echo $json;
The output is the original JSON, except for the duplicate object member. There are no other problems:
[
{
"id": 1535497,
"tid": 6970000,
"text": "Hello :) error is back! It's quite annoying!",
"dFlag": 1,
"iFlag": null,
"rFlag": null,
"member": null,
"contact": {
"id": 187,
"name": "User Name",
"_info": {
"contact_href": "https:\/\/url\/187"
}
},
"cFlag": null,
"processNotifications": null,
"dateCreated": "2015-12-08T13:59:19Z",
"createdBy": "User Name [email protected] (email)",
"eFlag": 1,
"_info": {
"lastUpdated": "2015-12-08T13:59:19Z",
"updatedBy": "User Name [email protected] (email)"
}
}
]
Now this you can decode again:
$back = json_decode($json);
print_r ($back);
The output is again the originalprint_r
output you provided, except for the duplicate member. So there is no problem with JSON encoding whatsoever.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 8.0.2".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.