Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid JSON in jsonlint, but JSON.parse() not working

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}]}"

like image 568
Nikhil Bharadwaj Avatar asked Oct 24 '25 00:10

Nikhil Bharadwaj


2 Answers

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'
like image 71
Elias Soares Avatar answered Oct 25 '25 15:10

Elias Soares


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/

like image 35
Vitalii Chmovzh Avatar answered Oct 25 '25 15:10

Vitalii Chmovzh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!