This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
My data on server is in the form of JSON array of objects. If I want to print the first object, how can I access its first object? Format of data is:
[
{
"eventId": "8577",
"datasetId": "34",
"nodeId": "8076",
"typeId": "4",
"type": "TempAndHum",
"status": "Temp : 35, Hum : 83",
"datasetName": "test active set",
"mode": "shared",
"xmlFragment": "Absent",
"gpsLat": "-23.549999",
"gpsLong": "-46.633301",
"contributor": "SanatIITD",
"addedOn": "2015-04-21 08:03:16",
"updatedOn": "2015-04-21 08:03:16"
},
{
"eventId": "8576",
"datasetId": "34",
"nodeId": "8076",
"typeId": "4",
"type": "TempAndHum",
"status": "Temp : 34, Hum : 81",
"datasetName": "test active set",
"mode": "shared",
"xmlFragment": "Absent",
"gpsLat": "-23.549999",
"gpsLong": "-46.633301",
"contributor": "SanatIITD",
"addedOn": "2015-04-21 08:03:11",
"updatedOn": "2015-04-21 08:03:11"
},
{
"eventId": "8575",
"datasetId": "34",
"nodeId": "8076",
"typeId": "4",
"type": "TempAndHum",
"status": "Temp : 33, Hum : 80",
"datasetName": "test active set",
"mode": "shared",
"xmlFragment": "Absent",
"gpsLat": "-23.549999",
"gpsLong": "-46.633301",
"contributor": "SanatIITD",
"addedOn": "2015-04-21 08:03:05",
"updatedOn": "2015-04-21 08:03:05"
},
]
I tried with data[0], but end up with printing "[". I tried with JSON.stringify also and then split with "," but that gives me first element of the object, not the whole first object. I need to print the whole first object which is within the curly braces. Is there any way so that I can access like array and by doing document.getElementById("Id")=myData[0], so that I don't need to print all elements within the curly braces separately. Please suggest me some solution. Thanks in advance.
use var dataArray = JSON.parse(dataString) first,
your data is a string at the moment.
Then use dataArray[0]
Related
This is a JSON snip that is created in WooCommerce by a plugin that adds some metadata. I cannot change the formatting of this JSON because it is generated by a plugin. The problem is that the keys and the values are added in a weird way. I am iterating through the line_items and statically referencing this data which I don't want to do, I would like to know if there is a smart way to reference for example:
"key": "_cpo_product_id",
"value": "3572",
if this was formatted correctly it would be: "_cpo_product_id": "3572" and not have "value" as a key, and it would be accessed by: foo.line_items[i]._cpo_product_id
but with this configuration I am a bit lost, I am sure there is an easy way to find the value for a specific key. I am doing this on Google app scripts, but a solution in JavaScript should suffice.
JSON snip:
"line_items": [
{
"id": 749,
"name": "Dune",
"product_id": 3572,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"subtotal": "149.54",
"subtotal_tax": "31.40",
"total": "149.54",
"total_tax": "31.40",
"taxes": [
{
"id": 24,
"total": "31.403148",
"subtotal": "31.403148"
}
],
"meta_data": [
{
"id": 11919,
"key": "_cpo_product_id",
"value": "3572",
"display_key": "_cpo_product_id",
"display_value": "3572"
},
{
"id": 11920,
"key": "_add-to-cart",
"value": "3572",
"display_key": "_add-to-cart",
"display_value": "3572"
},
I have a large json data that I have to parse and get object out of the following Json string. I added just top part of that large json data. I tried to parse it by
let obj = JSON.parse(this.state.data);
it doesn't work, it breaks with this msg "SyntaxError: Unexpected token L in JSON at position 0".
If I get the console.log by console.log(JSON.stringify(this.state.data, null, 2)); and try to validate output by online JSON validator, it says it is valid JSON data. Could you please tell me how can I parse it?
{
"content": [
{
"_id": 1,
"name": "Warehouse A",
"location": {
"lat": 47.13111,
"long": -61.54801
},
"cars": {
"location": "West wing",
"vehicles": [
{
"model": "GX",
"price": 27395.26,
"licensed": false,
"_id": 15,
"make": "Lexus",
"year_model": 2005,
"date_added": "2017-11-12T00:00:00.000+00:00"
},
{
"model": "Q",
"price": 6103.4,
"licensed": false,
"_id": 9,
"make": "Infiniti",
"year_model": 1995,
"date_added": "2017-11-13T00:00:00.000+00:00"
},
.........xxxxxx continue
Your variable this.state.data is not a JSON. It's a Javascript Object, just like JSON, So, Why not just use let obj = this.state.data;
Good day! Could anyone suggest please, is it possible to serialize/unserialize array of object in JavaScript and do not lose nested arrays? I want to save it into the database but when I unserialize json I lose prices array.
I use JSON.stringify and JSON.parse.
const arr = [{
"HotSpotData": "<SchematicsDefinition><Schema2D><Rectangles><RectangleDefinition ZOrder=\"0\" Top=\"440\" Left=\"176\" Width=\"27\" Height=\"27\" ShapeType=\"2\"/></Rectangles></Schema2D></SchematicsDefinition>",
"Ptr": 24580100323769,
"Ptr": 24588687801988,
"Ptr": 24773374403522,
"Qty": 1,
"Desc": "",
"Ptr": 24588687801988,
"Reference": 1,
"id": 25,
"notes": "",
"Code": "1831001",
"Description": "REEL CARRIER ",
"Ptr": 24558623176188,
"Notice": "",
"Order": 1,
"PartImage": null,
"prices": [{
"pricePtr": 24648817311745,
"priceValue": 1970.9,
"priceCaption": "USD LIST",
"currency": "USD"
}],
"rnd_unic": 1597222193169
}]
console.log(JSON.parse(JSON.stringify(arr)))
This question already has answers here:
Unable to access JSON property with "-" dash [duplicate]
(5 answers)
Closed 5 years ago.
So i am trying parse a json file. I am using alert to get the value of a particular field but whenever i pass this particular string it is displayed as nan
myjsondata=JSON.parse(json);
alert(myjsondata.result.parameters.College-name);
Json file
`{
"id": "1",
"timestamp": "2017-05-11T04:03:26.008Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "hi",
"action": "input.welcome",
"actionIncomplete": false,
"parameters": {
"College-name": "Apex Technical School"},
"contexts": [],
"metadata": {
"intentId": "b11a9493-7c2f-47c0-9928-5653a10c86e9",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Default Welcome Intent"
},
"fulfillment": {
"speech": "Hi welcome from webfocus Api Ai",
"messages": [
{
"type": 0,
"speech": "Hi welcome from webfocus Api Ai"
},
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "04737548-a3ff-485d-af1a-304edfee9486"
}`
alert with action and other fields are working fine . But for college it is displayed as NAN
You are getting a null value because parameters does not have a key called College-name:
"parameters": {},
Also, you might want to change your selection be using:
alert(myjsondata.result.parameters['College-name']);
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
(Sorry if the title doesn't make much sense, I couldn't figure out how to word it correctly)
So I'm trying to get the response from an API, but the API returns multiple responses with the same name.
Ex of a response:
{
"xuid": 2535436668322645,
"state": "Online",
"devices": [
{
"type": "XboxOne",
"titles": [
{
"id": 714681658,
"name": "Home",
"placement": "Background",
"state": "Active",
"lastModified": "2016-11-22T23:45:08.8296994Z"
},
{
"id": 74304278,
"activity": {
"richPresence": "Lvl 30 in Hudson Yards"
},
"name": "Tom Clancy's The Division",
"placement": "Full",
"state": "Active",
"lastModified": "2016-11-22T23:45:08.8296994Z"
}
]
}
]
}
So I was wondering, how would I be able to get the ID from the "second" response using JavaScript? (The one that says "Tom Clancy's The Division" in the name part) Thanks!
This should get you on the right track (untested)
var obj = jQuery.parseJSON(YOUR_JSON);
obj->devices[0]->titles[1]->id // second id