Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON returned from Response.json() appears to be invalid?

Checking the JSON output of my application on https://jsonlint.com/ indicates that the JSON is invalid… But I cannot fathom how it’s invalid.

The JSON in question:

{
    records: [{
        id: 70,
        whse: '00',
        partNo: '100E',
        description: '1" EMT CONDUIT (BUNDLE QTY. 1000FT)',
    }],
    start: 0,
    limit: 10,
    count: 1
}

I’ve tried changing the type of quotes used to no avail. The error returned doesn’t really help me at all either:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error: Parse error on line 1:
{   records: [{     id: 70
--^
Expecting 'STRING', '}', got 'undefined'

As far as I can tell, it’s valid JSON. This JSON is being returned from a Response.json() function call after a successful fetch().

>Solution :

You were right to think of the quotes as a potential source of problem.

That’s a valid JavaScript Object yes. Meaning a JS interpreter will successfully understand it if you were to do:

const variable = {
    records: [{
        id: 70,
        whse: '00',
        partNo: '100E',
        description: '1" EMT CONDUIT (BUNDLE QTY. 1000FT)',
    }],
    start: 0,
    limit: 10,
    count: 1
};

but that’s not a valid JSON string. if you want valid JSON string, you’d need to stringify it like this:

JSON.stringify({
    records: [{
        id: 70,
        whse: '00',
        partNo: '100E',
        description: '1" EMT CONDUIT (BUNDLE QTY. 1000FT)',
    }],
    start: 0,
    limit: 10,
    count: 1
});

and the result would be the string:

{
  "records": [
    {
      "id": 70,
      "whse": "00",
      "partNo": "100E",
      "description": "1\" EMT CONDUIT (BUNDLE QTY. 1000FT)"
    }
  ],
  "start": 0,
  "limit": 10,
  "count": 1
}

(notice the double quotes around key names)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading