[Solved] Is this a correct JSON string?


Your “JSON string” seems to be JavaScript code that you would have in a script tag that creates a drivers JavaScript object with a property called driver that is an array of other objects. To be JSON, you remove the var drivers = and the semicolon at the end leaving just what you would set a variable to in order to create that object.

For instance, let’s say you have a “person” variable that is an object with “firstName” and “lastName” properties. This is code you would put into a script to create that variable:

var person = { firstName: "Jason", lastName: "Goemaat" };

Strings, even JSON strings are represented in scripts surrounded by single or double quotes. To see the string value for the JSON of that object, you can do this:

var json = JSON.stringify(person);

This gives you a valid JSON string which will display this in the console:

{"firstName":"Jason","lastName":"Goemaat"}

To set the variable directly you need to enclose it in single or double quotes, but if the string contains those they would need to be escaped. Since this contains only double quotes I will surround it in single quotes to assign it to a variable in my script, then parse that into an object.

var json = '{"firstName":"Jason","lastName":"Goemaat"}';
var person = JSON.parse(json);

JSON property names need to be surrounded in quotes. Most parsers will work if they are not but it is always better to be as correct as you can be. You can use the online tool at jsonlint to check for valid JSON.

1

solved Is this a correct JSON string?