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)))
Related
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;
I want to delete an object from a nested array by comparing another field in the Mongo database by using mongoose in javascript. Here is my schema.
{
"userId": "5e80e3021c5c461433807173",
"selected": [
{
"productId": "5e80e664602315159a2959da",
"selectedQty": 1,
"isAvailable": true,
"name": "Nike Mountain Shoe",
"category": "Shoes",
"qty": 2497,
"price": 1500,
"imageUrl": "aHR0cHM6Ly93d3cucmFua2FuZHN0eWxlLmNvbS9tZWRpYS9wcm9kdWN0cy9qL2phbWVzLXBlcnNlLWxvbmctc2xlZXZlLXQtc2hpcnQtbWVucy1sb25nLXNsZWUuanBn"
},
{
"productId": "5e80e4893db96d14947dce80",
"selectedQty": 1,
"isAvailable": false,
"name": "Blimper Men Shoe",
"category": "Shoes",
"qty": 2497,
"price": 500,
"imageUrl": "aHR0cHM6Ly93d3cucmFua2FuZHN0eWxlLmNvbS9tZWRpYS9wcm9kdWN0cy9qL2phbWVzLXBlcnNlLWxvbmctc2xlZXZlLXQtc2hpcnQtbWVucy1sb25nLXNsZWUuanBn"
}
]
}
How can I delete a product by comparing userId and productId?
I have a JSON array that looks like this:
{
"id": 258,
"rawId": null,
"displayName": null,
"name": {
"givenName": "my ame",
"honorificSuffix": "",
"formatted": "my ame",
"middleName": "",
"familyName": "",
"honorificPrefix": ""
},
"nickname": "",
"phoneNumbers": [{
"value": "23423442342424",
"pref": false,
"id": 0,
"type": "mobile"
}],
"emails": null,
"addresses": null,
"ims": null,
"organizations": [{
"pref": "false",
"title": "",
"name": "",
"department": "",
"type": null
}],
"birthday": null,
"note": "",
"photos": null,
"categories": null,
"urls": null
}
I need to get the phoneNumbers >> value from this JSON.
SO I TRIED SOMETHING LIKE THIS:
var d = JSON.parse(test);
alert(test[0].phoneNumbers.value);
The variable test is the JSON shown above.
and I also tried:
alert(d[0].phoneNumbers.value);
and
alert(test.phoneNumbers.value);
But none of the above work.
Is there something that I am missing in my code?
Thanks in advance.
What you showed us is a JSON string (giving a JS object after parsing), not an array.
So d[0].phoneNumbers will not work and d.phoneNumbers will work and will give you an array.
And because it will give you an array, d.phoneNumbers.value will not work, and d.phoneNumbers[0].value will.
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]
Ok, so I am trying to sort out my json data into a table:
{
"0": {
"1": {
"airdate": "2011-12-04",
"file_size": 368279154,
"location": "filepath",
"name": "episodename",
"quality": "Unknown",
"release_name": "",
"status": "Downloaded",
"subtitles": ""
}
},
"1": {
"1": {
"airdate": "2011-12-04",
"file_size": 368279154,
"location": "filepath",
"name": "episodename1",
"quality": "Unknown",
"release_name": "",
"status": "Downloaded",
"subtitles": ""
},
"2": {
"airdate": "2011-12-04",
"file_size": 368279154,
"location": "filepath",
"name": "episodename2",
"quality": "Unknown",
"release_name": "",
"status": "Downloaded",
"subtitles": ""
},
"14": {
"airdate": "2011-12-04",
"file_size": 368279154,
"location": "filepath",
"name": "episodename14",
"quality": "Unknown",
"release_name": "",
"status": "Downloaded",
"subtitles": ""
}
}
The problem is my angular ng-repeat lists it in order alphabetically ex 1, 14, 2... I can't seem to work with this object to take the key and set it as an ID parameter for the object but I thought that would be the best option. any suggestions would be helpful. I would like to note I cannot alter the server side api.
if the order of some collection is important, you should consider using array instead.
if you stick using object, the way is to create an array to keep the object's keys for orderfing purpose
the prototype
scope.obj = {"1":{}, "2":{}, "14":{}};
scope.objKeys = ['1', '2', '14'];
<div ng-repeat="key in objKeys">
{{obj[key]}}
</div>