Passing json data from an api to a javascript variable

api, javascript, json

Solution

Put your JSON string a variable `var jsonString`

then...

var myJsonObject = JSON.parse(jsonString);

Problem

Am a newbie in json/javascript so please bear with me. Am getting json data from an api url that loads sth like ``` { "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] ``` i need it as a javascript variable on a script file ``` var employess={ "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] } ``` Ave tried ``` $.getJSON(myUrl",function(data){ $("#selector").data("JSONP",data); alert(data); }); ```

Original source