'JSON parse a property of an object inside an object?
I'm trying to JSON.parse(nodeInfluxSeries)
a property that is in an object, that is also in an object and in an array.
Like so:
Array [
Object {
"id": 1,
"properties": Object {
"nodeInfluxSeries": "[{\"database\": \"boba\", \"fields\": [], \"measurement\": \"boba\", \"retentionPolicy\": \"boba\", \"tags\": {\"nodeId\": \"boba\"}}]",
"series": "",
"version": "",
},
"userRights": Object {
"monitorManagement": true,
"propertyEdit": Object {},
},
},
]
Tried something like this but it places a new property inside the first object.
note: random is the array
random.map(r => {
return {
...r,
nodeInfluxSeries: JSON.parse(c.properties.nodeInfluxSeries),
};
})
Solution 1:[1]
You need to nest the JSON.parse()
inside the properties
property of the result.
random.map(r => {
return {
...r,
properties: {
...r.properties,
nodeInfluxSeries: JSON.parse(r.properties.nodeInfluxSeries)
}
};
})
You can also update the property in place instead of recreating all the objects:
random.forEach(r => r.properties.nodeInfluxSeries = JSON.parse(r.properties.nodeInfluxSeries));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Barmar |