I have the following json response from a php file
{
"id":"85",
"title":"Hello,
hello I said",
"tags":null,
}
the correct output in the "title" should be: "title":"Hello, hello I said", bla bla bla.
Notice that the comma after the Hello.
i am using json_encode() but I still get the above result (which breaks the json).
here is my php code:
$query = mysql_query("SELECT * from songs");
$rows = array();
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
}
$songArray = array(
"songs" => $rows
);
$jsonD = json_encode($songArray);
If I don't include the commas in my input everything works fine.
The code should work, there is nothing special in it.
Does anyone have any ideas why this is happening? How can I be fixed. thank you
{ "id":"85", "title":"Hello, hello I said", "tags":null, }
is invalid json. You need to remove the comma after null,
{ "id":"85", "title":"Hello, hello I said", "tags":null }
You should remove the extra ,
after "tags":null,
-- most browsers will accept this JSON, but I believe certain IE versions will puke on this.
The primary problem you may be having is it looks like you have a line break in:
"title":"Hello,
hello I said",
If that is a true line break (eg., char 13 or char 10), then javascript will not be happy (unterminated string literal). The fix is to use PHP string replace functions to replace any occurrences of char 13 or 10 with a replacement character, probably just a space. Here is an example using preg_replace():
A quick fix to your problem would be using rtrim like the following.
$json = rtrim(json_encode($songArray), ',}') . '}';
and if your Json has a line break before the closing bracket using the following would fix that.
$json = rtrim(json_encode($songArray), ",
}") . "
}";
or you could use a preg replace like below.
$json = preg_replace("/,}/", '}', json_encode($songArray));
Pass the json as
{
"id": "85",
"title": "Hello,hello I said",
"tags": ""
}
I validate the json in a online json validator there were two errors the first one was the 3rd line of your json, it cam because you put an enter after the "Hello,". the second one was because of the null you put in the fourth you can use the link below to validate the json.
found the problem. i was using a function that make the json easy to read. It turns out that it was the function's issue. It broke the json everytime it found a comma therefore, it messed everything up. i took the function out and every is working as it supposed to be.
thank you all for your time and input.
It is true that this is invalid json.
However, if you want to parse those bad jsons anyway, and you need multidimensional you can use:
json_decode(preg_replace('/,\s*}/','}',$config_json));
Note that it will replace any occurencies of ',}' with } so be careful if by any chance strings inside jsons may contain them.
feg.
{
"option1": 20,
"more_info": {
"the_weird_string": "the string that, } somehow contains this",
},
"option2": 10,
}
will return
[the_weird_string] => the string that} somehow contains this