mhr
mhr

Reputation: 135

php json get comments

How do I get the comments in a json string using php?

I have a php script that decodes data in a json string, but there is an additional small piece of metadata included as a comment in the first line of the string. I could split the string and get the first line, and parse that with regex, but I'd rather use a native json function for getting the comments if it exists.

Anybody knows something like this?

Upvotes: 1

Views: 421

Answers (2)

cweiske
cweiske

Reputation: 31068

The JSON spec does not define comments, which means that your JSON string is not JSON. PHP thus has no native way to extract comments from json strings.

Upvotes: 1

Quentin
Quentin

Reputation: 943108

The JSON specification does not allow for comments. If your data includes comments, then it is not JSON and a JSON parser should be expected to error on encountering them.

It sounds like you will have to preprocess your non-JSON data to split it into a JSON part and a comment part, then process the JSON part normally and the comment part with some custom code.

Upvotes: 2

Related Questions