0

why is this true:

  import json
  json.loads('{"A":2}')

but this is wrong:

  json.loads('{"A":2,}')

OR

  json.loads("['A':2]")
1
  • 2
    Because {"A":2,} (trailing comma) and ['A':2} ([],{} mismatch, key not in double quotes) are not valid JSON. See json.org and jsonlint.com. Commented Apr 30, 2012 at 0:13

2 Answers 2

3

JSON is a subset of JavaScript. This means things that are valid JavaScript are not necessarily valid JSON.

  • {"A":2,} is valid JS (except in old IE versions), but not valid JSON
  • ['A':2} is not even valid JS since the braces do not match. If they matched, it would still be invalid JSON as JSON always uses " and never ' to quote strings.

See http://json.org/ for the JSON specs.

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

2 Comments

oops , i mean json.loads("['A':2]")
@mehRad: Still wrong, because of (a) key is not in double quotes and (b) the : is invalid there. Either define an object: {k:v, k:v, ...} or an array [v, v, ...]. That's how JSON is defined, you just have to remember it. Therefore: json.org
1

Because the last two options are not a valid json

  • There should be no comma after last element
  • a [ should match a ] (array) and a { should match a } (object)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.