How do you load json from a string in javascript?

By On

I have a json string that is a regular raw string and job a javascript object. How do i load this json into a javascript object so that I can use it normally? 

var jsonStr = "{\"property1\": \"test\", \"property2\": \"something else\" }";
alert(loadedJson.property1);

The example above is a basic idea of what I am trying to do. I want to load json from a string and be able to access the properties of this object as if it were a regular javascript object.

1 Answer

- Advertisment -
SEMrush
Answered By Keven Krok On

To load json from a string, you can use the build in JSON handler in JS. Do this you can call the parse method.

var jsonStr = ""{""property1"": ""test"", ""property2"": ""something else"" }"";
var loadedJson = JSON.parse(jsonStr);
alert(loadedJson.property1);