how can i take this json data (either from website or pasted into local file) and read it into a javascript object?
Here is the json data: https://jsonblob.com/b914b62a-e276-11e7-8a9c-f99a86745e2b
You can convert an JSON file to an object by using JSON.parse
let convertedObject = JSON.parse(text)
There are not many differences between a JSON string and a Javascript Object. In fact Everything is an object in javascript, from functions to arrays...well coming back to your question you can get the value stored in a JSON string by parsing the JSON string by using JSON.parse() function and then call it as you generally do with JS objects. for Example: -
var jsonString = JSON.stringify({key: "val"}); // here i am converting a regular object to JSON string
var jsonToObj = JSON.parse(jsonString);
console.log(jsonToObj.key);
Related
var x=[{"name":"james","age":"23"},{"name":"job","age":"55"}];
How to convert array of json object to below response
{{"name":"james","age":"23"},{"name":"job","age":"55"}};
let x=[{"name":"james","age":"23"},{"name":"job","age":"55"}];
console.log(JSON.stringify(x));
The desired output isnt proper JSON. To convert any javascript object into a json string simply use the function JSON.stringify(obj). JSON.parse(string) can be used to create an object out of a string.
I am trying to access the t part of the "data": object below. I am doing this by doing console.log(message.data.f) however this returns undefined. I do not understand why I cannot access it in this way. See object below:
"data":"{\"e\":\"53845\",\"f\":\"SCORE\",\"pf\":[{\"p\":\"HOME\",\"v\":\"0\"},{\"p\":\"AWAY\",\"v\":\"0\"}],\"^t\":\"f\",\"i\":\"357575\",\"z\":1492771602631}",
Note I have marked the part of the object I wish to access with a ^
Your data property is a JSON string and probably all the object is a JSON string.
You need to parse the string as JSON
var obj = JSON.parse(myObj.data);
and then you can access:
console.log(obj.f);
If your first object, the one containing data, is not already a JSON too and its name is for example myFirstObject you need to do just this:
var jsonObj = JSON.parse(myFirstObject);
console.log(jsonObj.f);
Your message is nothing but string. Parse it first to a corresponding object to access its variables.
var parsed = JSON.parse(message);
console.log(message.data.t);
I am passing data back to java script through a rest api. The rest API return data in the following below mentioned structure.I am confused what format of json is it and how can i iterate on the array inside and get the int value individually.
The format of my response is like :
{"searchOrder":[
{"location":"10","trackingId":"656"},
{"location":"34","trackingId":"324"},....],
"count":100}
i want searchOrder as array of json to display in table and count to show total row.How can i get them?
Just iterate over the array and extract the values
// Turn the JSON into a JS object (maybe in your XHR handler)
var data = JSON.parse(jsonString); // jsonString could be responseText from XHR
var arrayOfTrackingIdValues = data.searchOrder.map(function (value) {
return value.trackingId;
});
JavaScript provide built-in function JSON.parse() to convert the string into a JavaScript object and drive/iterate the each element based on type obj/array
var obj = JSON.parse(text);
I have a servlet where it returns a string like [["one","two"],["one","two"]], which is an array representation.
This was sent to the front end using the Gson library's toJson() method as a String. It sends this String array along with some other attributes in JSON format and then I'm using the toJson() method to send it to front end.
In the Ajax response handling, I'm doing a $.parseJSON() on the object and retrieving the values. (It works fine and I was able to retrieve that array string.)
Now in JavaScript I need to create another JSON object with some other values along with this string array and send it to another JavaScript method. But in that method I cannot retrieve the values. My motive is to extract this array string as an array and iterate it.
Following is what I do in JavaScript to send to JavaScript method:
var streamId = stream + CONSTANTS.colon + streamVersion;
var eventsData = {};
var jsonData = [];
eventsData ["source"] = streamId;
eventsData ["data"] = eventsArray;
jsonData.push(eventsData);
onSuccessFunction(jsonData);
In onSuccessFunction I tried to get jsonData.data, but it says undefined.
Any suggestions?
When we can read a property directly from string:
var data = {"id":1,"name":"abc","address":{"streetName":"cde","streetId":2}};
console.log(data.address.streetName); // cde
Why do people use JSON.parse:
var obj = JSON.parse(data);
console.log(obj.address.streetName); // cde
It is not a string, but Javascript object. String is given below
var data = '{"id":1,"name":"abc","address":{"streetName":"cde","streetId":2}}';
to make it object we use JSON.parse
var obj = JSON.parse(data);
console.log(obj.address.streetName); // cde
In your first example, data is an object, but in your second example, data is a JSON string.
That's a major difference. You could call eval(data) to parse a JSON string, but that's very unsafe.
JSON.parse() expects a string. More specifically, a string with a JSON-encoded piece of data.
If it's applied to an object then it's an error, the source of which is probably the common confusion that seems to exist between JavaScript objects and the JSON format.