Home > Net >  JSON parse a property of an object inside an object?
JSON parse a property of an object inside an object?

Time:05-14

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),
      };
    })

CodePudding user response:

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));
  • Related