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?

5
  • 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. Commented Mar 10, 2015 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). Commented Mar 10, 2015 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? Commented Mar 10, 2015 at 11:31
  • 1
    Have a look at this stackoverflow.com/a/1575315/1301076 it might help to unscrew things if you can't fix it at the source Commented Mar 10, 2015 at 11:36
  • Yup, that will do it. Commented Mar 10, 2015 at 11:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I am aware of that. I am looking for a solution.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.