2

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

user2062455
  • 431
  • 5
  • 14

6 Answers6

2

{ "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 }

DhruvPathak
  • 40,405
  • 15
  • 109
  • 170
  • He didn't write it by hand... it's clearly stated this is the output of `json_encode`. – Okonomiyaki3000 Feb 04 '14 at 06:31
  • 2
    @Okonomiyaki3000 , This cannot be the output of json_encode, even if the original array contains incorrect comma. json_encode has been designed to output valid json. demo : http://codepad.org/uVyH9aEh – DhruvPathak Feb 04 '14 at 07:06
  • it is strange, but this is part of the output of json_encode. it doesn't make sense to me either. – user2062455 Feb 04 '14 at 07:27
2

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():

Remove new lines from string

Community
  • 1
  • 1
David Fleeman
  • 2,553
  • 12
  • 16
  • there are no line breaks. I have other line breaks in a textarea that work ok. this is the only issue, with the comma. – user2062455 Feb 04 '14 at 07:26
  • Sounds like you may need to simply test for the case of a trailing comma after you do the json_encode and manually delete it in the PHP. Not ideal. Here is more info on the trailing comma: http://stackoverflow.com/questions/201782/can-you-use-a-trailing-comma-in-a-json-object – David Fleeman Feb 04 '14 at 07:38
0

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), ",\n}") . "\n}";

or you could use a preg replace like below.

$json = preg_replace("/,}/", '}', json_encode($songArray));
TURTLE
  • 3,510
  • 4
  • 47
  • 48
0

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.

http://jsonlint.com/

user2655318
  • 67
  • 4
  • 14
0

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.

user2062455
  • 431
  • 5
  • 14
  • For any others who run into this using custom functions: you need to make sure loops have a counter + total, and a clause to remove the comma on the last iteration of a depth. Ie, `($total_rows > $iteration) ? ',' : ''` – dhaupin Feb 10 '16 at 16:59
0

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