4

I have a JSON which is verified in the JSONlint, but I cannot use JSON.parse() as it is not working. What is the problem with the JSON here, if JSON.prase() cannot be used what are my alternatives.

JSON string : "{Products: [{Id: 1,Increment: 5,Max: 1000,Min: 25,allowed: false,Desc: product description,Name:Product Name,Qty: 0}]}"

3
  • is there an error message? Commented Feb 13, 2018 at 19:03
  • 2
    Oh, that is not valid JSON.... none of the keys are quoted. Not sure how that would have passes a JSON linter. Commented Feb 13, 2018 at 19:04
  • Yeah, same here, not valid! Commented Feb 13, 2018 at 19:04

3 Answers 3

3

To a JSON be valid, your object keys must be inside double quotes:

{ "validKey": 123 }
  ^        ^
  |        |
  ------------- These double-quotes are required!

JSONLint said that it's alright because you pasted the JSON as you pasted here, wrapped in quotes:

"{Products: [{Id: 1,Increment: 5,Max: 1000,Min: 25,allowed: false,Desc: product description,Name:Product Name,Qty: 0}]}"

And this is a json string with a JSON inside, not a JSON!

If you try to JSONLint without the Quotes you will get this error:

Error: Parse error on line 1:
{   Products: [{        Id: 1
--^
Expecting 'STRING', '}', got 'undefined'
Sign up to request clarification or add additional context in comments.

Comments

1

Your strings and keys should be quoted. This is valid JSON that will be correctly parsed by JSON.parse()

{
  "Products": [
    {
      "Id": 1,
      "Increment": 5,
      "Max": 1000,
      "Min": 25,
      "allowed": false,
      "Desc": "product description",
      "Name": "Product Name",
      "Qty": 0
    }
  ]
}

You can read more about the standard here: https://www.json.org/

Comments

0

This can also happen when you are passing the JSON content inside a "string" variable, you need to using single quotes insted of double quotes of the outside string

var data = "{ "nodes": [{ "id": 1, "text": "DefinitionFinder" },{ "id": 2, "text": "ProjectReference" },{ "id": 3, "text": "ReferenceManager" },{ "id": 4, "text": "ReferenceType" },{ "id": 5, "text": "EventStream" },{ "id": 6, "text": "AutoDisposable" },{ "id": 7, "text": "Handler" }], "links": [{ "from": 1, "to": 3 },{ "from": 2, "to": 4 },{ "from": 3, "to": 1 },{ "from": 3, "to": 2 }] }"

This is the correct way

var data = '{ "nodes": [{ "id": 1, "text": "DefinitionFinder" },{ "id": 2, "text": "ProjectReference" },{ "id": 3, "text": "ReferenceManager" },{ "id": 4, "text": "ReferenceType" },{ "id": 5, "text": "EventStream" },{ "id": 6, "text": "AutoDisposable" },{ "id": 7, "text": "Handler" }], "links": [{ "from": 1, "to": 3 },{ "from": 2, "to": 4 },{ "from": 3, "to": 1 },{ "from": 3, "to": 2 }] }'

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.