0

PHP's JSON decode requires a JSON formatted with field names enclosed in double quotes like:

{"foo-bar": 12345, "myName": "Johnnie"}

But I have to parse some relatively complex JSON made by a Java application that is formatted with names without double quotes on field names like:

{foo-bar: 13456, myName: "Johnnie"}

...but more complex. Is there some easy way to fix this or am I screwed?

TimSim
  • 3,936
  • 7
  • 46
  • 83
  • 1
    Which json parser are you using on the Java side? The correct way would be with the double quotes. If you are using Jackson, it can be configured. – facundofarias Mar 10 '15 at 11:29
  • 2
    The requirement you describe is a requirement of JSON, not PHP. The data generated by the Java application simply isn't JSON. It isn't even valid JavaScript syntax (since identifiers can't have hyphens in them). – Quentin Mar 10 '15 at 11:29
  • I'm not sure which parser is used on the Java side, I just got the incorrectly formatted data. As for hyphens, that's just from the PHP website, I don't think any field names have hyphens in the strings I got. Can PHP parse Javascript in a similar way? – TimSim Mar 10 '15 at 11:31
  • 1
    Have a look at this http://stackoverflow.com/a/1575315/1301076 it might help to unscrew things if you can't fix it at the source – rjdown Mar 10 '15 at 11:36
  • Yup, that will do it. – TimSim Mar 10 '15 at 11:38

1 Answers1

0

This requirement follows the JSON specification. If you look at the charts at http://json.org/ you will see that an object consists out of string:value pairs.

JSON object description

A string is defined to have quotes enclosing Unicode characters.

JSON sring description

The data you have is not valid JSON.

johannes
  • 15,807
  • 3
  • 44
  • 57
  • Yes, I am aware of that. I am looking for a solution. – TimSim Mar 10 '15 at 11:37
  • Fix the data or write your own parser, built-in PHP doesn't provide anything that really helps. Maybe a less restrictive parser exists already? - I don't know. – johannes Mar 10 '15 at 11:39