hash_ir hash_ir - 1 year ago 131
JSON Question

JSON string parser without literals

How to check if a string like this

, without any literals, can be represented as a JSON object or not?

The input comes with the following constraints:
1. A JSON object should start with '{' and ends with a '}'.
2. The key and value should be separated by a ':'.
3. A ',' suggests an additional JSON property.
4. An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself.

And it is to be intrepreted like this:

"Key": [{
"Key": "Value"
}, {
"Key": "Value"

Answer Source

The syntax spec for JSON can be found here.

It indicates that the [{},{}] is legal, because [] has to contain 0 or more elements separated by ,, and {} is a legal element. However, the first part of your example is NOT valid - the : must have a string in front of it. While it is legal for it to be an empty string, it's not legal for it to be null, and the interpretation of a totally missing element is ambiguous.

So. {"":[{},{}]} is legal, but {:[{},{}]} is not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download