Help with parsing JSON

Can someone help me to extract
"originalDurationInMillis": 335000
from this JSON string? Thanks

{
  "topic": "sensor.stephen_next_timer",
  "payload": "2022-02-05T23:39:36-05:00",
  "data":
    {
      "entity_id": "sensor.stephen_next_timer",
      "state": "2022-02-05T23:39:36-05:00",
      "attributes":
        {
          "recurrence": "Never Repeat",
          "process_timestamp": "2022-02-05T23:34:03.046591-05:00",
          "prior_value": "2022-02-05T23:39:37-05:00",
          "total_active": 1,
          "total_all": 4,
          "sorted_active": '[["92cbfc57-6006-36ab-8ad1-5ea30263d9fc", {"alarmLabel": null, "alarmTime": 0, "assigner": null, "automationId": null, "createdDate": 1644122041127, "deferredAtTime": null, "deliveryType": null, "deviceName": null, "deviceSerialNumber": "G0911B0592850CN1", "deviceType": "A4ZP7ZC4PI6TO", "extensibleAttribute": null, "geoLocationTriggerData": null, "id": "A4ZP7ZC4PI6TO-G0911B0592850CN1-92cbfc57-6006-36ab-8ad1-5ea30263d9fc", "lastOccurrenceTimeInMilli": 0, "lastUpdatedDate": 0, "loopCount": null, "mrmGroupContext": null, "musicAlarmId": null, "musicEntity": null, "notificationIndex": "92cbfc57-6006-36ab-8ad1-5ea30263d9fc", "originalDate": null, "originalDurationInMillis": 335000, "originalTime": null, "personProfile": null, "primaryDeviceState": null, "provider": null, "rRuleData": null, "recurrenceEligibility": false, "recurringPattern": null, "remainingTime": 333000.0, "reminderAppLabel": null, "reminderLabel": null, "reminderSubLabel": null, "skillInfo": null, "sleepTrackingAlarmProper...',
          "sorted_all": '[["956d96dd-f86a-35ea-9417-d8e7df937035", {"alarmLabel": null, "alarmTime": 0, "assigner": null, "automationId": null, "createdDate": 1644119027334, "deferredAtTime": null, "deliveryType": null, "deviceName": null, "deviceSerialNumber": "G0911B0592850CN1", "deviceType": "A4ZP7ZC4PI6TO", "extensibleAttribute": null, "geoLocationTriggerData": null, "id": "A4ZP7ZC4PI6TO-G0911B0592850CN1-956d96dd-f86a-35ea-9417-d8e7df937035", "lastOccurrenceTimeInMilli": 0, "lastUpdatedDate": 0, "loopCount": null, "mrmGroupContext": null, "musicAlarmId": null, "musicEntity": null, "notificationIndex": "956d96dd-f86a-35ea-9417-d8e7df937035", "originalDate": null, "originalDurationInMillis": 900000, "originalTime": null, "personProfile": null, "primaryDeviceState": null, "provider": null, "rRuleData": null, "recurrenceEligibility": false, "recurringPattern": null, "remainingTime": 0, "reminderAppLabel": null, "reminderLabel": null, "reminderSubLabel": null, "skillInfo": null, "sleepTrackingAlarmProperties": ...',
          "status": "ON",
          "dismissed": "2022-02-05T23:34:03.075791-05:00",
          "device_class": "timestamp",
          "icon": "mdi:timer-outline",
          "friendly_name": "Stephen next Timer",
        },
      "last_changed": "2022-02-06T04:34:03.077149+00:00",
      "last_updated": "2022-02-06T04:34:03.077149+00:00",
      "context":
        {
          "id": "edfadd86d60b2adce06ef67f1117d5d2",
          "parent_id": null,
          "user_id": null,
        },
      "timeSinceChanged": "12 seconds ago",
      "timeSinceChangedMs": 11809,
      "original_state": "2022-02-05T23:39:36-05:00",
    },
  "_msgid": "939d383111eeaa6e",
}

The JSON, your posted here, is not valid. It looks like the sorted_active property which contains you desired value "originalDurationInMillis" is not complete.

First, I would extract the sorted_active property into the payload.

Use a change node - to extract sorted_active

by setting msg.payload to msg.data.attributes.sorted_active . The value then should be a nested array

As Micky says, that i assume is a string that is not formatted as a Json string. it has single quotes and commas after the last object values, as well as missing ends to arrays ] and objects }.

To even parse it as an Json object it need some repairs. Where did it come from and do you have access to alter it?

Here is what i did to repair the Json format and return your value.

[{"id":"3e22833b.78740c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{  \"topic\": \"sensor.stephen_next_timer\",  \"payload\": \"2022-02-05T23:39:36-05:00\",  \"data\":    {      \"entity_id\": \"sensor.stephen_next_timer\",      \"state\": \"2022-02-05T23:39:36-05:00\",      \"attributes\":        {          \"recurrence\": \"Never Repeat\",          \"process_timestamp\": \"2022-02-05T23:34:03.046591-05:00\",          \"prior_value\": \"2022-02-05T23:39:37-05:00\",          \"total_active\": 1,          \"total_all\": 4,          \"sorted_active\": '[[\"92cbfc57-6006-36ab-8ad1-5ea30263d9fc\", {\"alarmLabel\": null, \"alarmTime\": 0, \"assigner\": null, \"automationId\": null, \"createdDate\": 1644122041127, \"deferredAtTime\": null, \"deliveryType\": null, \"deviceName\": null, \"deviceSerialNumber\": \"G0911B0592850CN1\", \"deviceType\": \"A4ZP7ZC4PI6TO\", \"extensibleAttribute\": null, \"geoLocationTriggerData\": null, \"id\": \"A4ZP7ZC4PI6TO-G0911B0592850CN1-92cbfc57-6006-36ab-8ad1-5ea30263d9fc\", \"lastOccurrenceTimeInMilli\": 0, \"lastUpdatedDate\": 0, \"loopCount\": null, \"mrmGroupContext\": null, \"musicAlarmId\": null, \"musicEntity\": null, \"notificationIndex\": \"92cbfc57-6006-36ab-8ad1-5ea30263d9fc\", \"originalDate\": null, \"originalDurationInMillis\": 335000, \"originalTime\": null, \"personProfile\": null, \"primaryDeviceState\": null, \"provider\": null, \"rRuleData\": null, \"recurrenceEligibility\": false, \"recurringPattern\": null, \"remainingTime\": 333000.0, \"reminderAppLabel\": null, \"reminderLabel\": null, \"reminderSubLabel\": null, \"skillInfo\": null, \"sleepTrackingAlarmProper...',          \"sorted_all\": '[[\"956d96dd-f86a-35ea-9417-d8e7df937035\", {\"alarmLabel\": null, \"alarmTime\": 0, \"assigner\": null, \"automationId\": null, \"createdDate\": 1644119027334, \"deferredAtTime\": null, \"deliveryType\": null, \"deviceName\": null, \"deviceSerialNumber\": \"G0911B0592850CN1\", \"deviceType\": \"A4ZP7ZC4PI6TO\", \"extensibleAttribute\": null, \"geoLocationTriggerData\": null, \"id\": \"A4ZP7ZC4PI6TO-G0911B0592850CN1-956d96dd-f86a-35ea-9417-d8e7df937035\", \"lastOccurrenceTimeInMilli\": 0, \"lastUpdatedDate\": 0, \"loopCount\": null, \"mrmGroupContext\": null, \"musicAlarmId\": null, \"musicEntity\": null, \"notificationIndex\": \"956d96dd-f86a-35ea-9417-d8e7df937035\", \"originalDate\": null, \"originalDurationInMillis\": 900000, \"originalTime\": null, \"personProfile\": null, \"primaryDeviceState\": null, \"provider\": null, \"rRuleData\": null, \"recurrenceEligibility\": false, \"recurringPattern\": null, \"remainingTime\": 0, \"reminderAppLabel\": null, \"reminderLabel\": null, \"reminderSubLabel\": null, \"skillInfo\": null, \"sleepTrackingAlarmProperties\": ...',          \"status\": \"ON\",          \"dismissed\": \"2022-02-05T23:34:03.075791-05:00\",          \"device_class\": \"timestamp\",          \"icon\": \"mdi:timer-outline\",          \"friendly_name\": \"Stephen next Timer\",        },      \"last_changed\": \"2022-02-06T04:34:03.077149+00:00\",      \"last_updated\": \"2022-02-06T04:34:03.077149+00:00\",      \"context\":        {          \"id\": \"edfadd86d60b2adce06ef67f1117d5d2\",          \"parent_id\": null,          \"user_id\": null,        },      \"timeSinceChanged\": \"12 seconds ago\",      \"timeSinceChangedMs\": 11809,      \"original_state\": \"2022-02-05T23:39:36-05:00\",    },  \"_msgid\": \"939d383111eeaa6e\",}","payloadType":"str","x":80,"y":4120,"wires":[["96484acf.b6dd18"]]},{"id":"96484acf.b6dd18","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"'[","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":", [^ ]* {0,1}\\.\\.\\.'","fromt":"re","to":"}]","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":",(\\s*})","fromt":"re","to":"$1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":4120,"wires":[["f9848009.e5d778"]]},{"id":"f9848009.e5d778","type":"json","z":"c791cbc0.84f648","name":"","property":"payload","action":"","pretty":false,"x":490,"y":4120,"wires":[["2682e1eb.fd139e"]]},{"id":"2682e1eb.fd139e","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.data.attributes.sorted_active[1].originalDurationInMillis","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":4080,"wires":[["b7c99b48.c4cf18"]]},{"id":"b7c99b48.c4cf18","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":4040,"wires":[]}]

Well, this explains why I couldn't find the value...
The data is from Amazon (Alexa) using a Home Assistant component, so I can't change the source.

I wonder if I could just treat it as a string in a function and simply search for the key?

Could you show us what the msg looks like in the debug window? (a screenshot with properties expanded)

This is a screenshot of what I am working with, but the tip by MickyM2 got me to a solution that I can work with.

My solution, messy as it is:

var w = msg.data.attributes.sorted_active;
msg.data = '';
msg.payload = w;
var output = msg.payload.split(",");
msg.payload = output[22];
var y = msg.payload.split(":");
msg.payload = y[1];
msg.payload = Number(msg.payload);
return msg;

Thanks everyone.

I suspected the value you pasted in the original post were not copied using the "copy value" button & that messed with the format when in fact the variable msg.data.attributes.sorted_active was actually valid JSON

If I'm right, then you can simply convert the JSON to an object.

Would you try...

msg.payload = JSON.parse(msg.data.attributes.sorted_active)
return msg;

Then it should be an object & you can use the copy path button to access the value.

Canned text (in case you weren't aware)...

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

I thought I did use the "copy value" button.

See how there is a single quote after the colon, that makes it invalid JSON.

Now look at the screenshot, the payload is an object and sorted_active is what appears to be a string containing valid JSON.

Did you try what I suggested?

Steve is correct your original data was not copied using the debug copy value button.
As the following could not be outputted by the debug node.

 "friendly_name": "Stephen next Timer",
        },

It has a comma after last value. The single quote and following comma are valid in a function node though.
Try what Steve has pointed out
e.g.

[{"id":"3e22833b.78740c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":90,"y":4080,"wires":[["f81e1cb3.6ae78"]]},{"id":"f81e1cb3.6ae78","type":"function","z":"c791cbc0.84f648","name":"simulated correct data","func":"msg = {\n    \"topic\": \"sensor.stephen_next_timer\",\n    \"payload\": \"2022-02-05T23:39:36-05:00\",\n    \"data\": {\n        \"entity_id\": \"sensor.stephen_next_timer\",\n        \"state\": \"2022-02-05T23:39:36-05:00\",\n        \"attributes\": {\n            \"recurrence\": \"Never Repeat\",\n            \"process_timestamp\": \"2022-02-05T23:34:03.046591-05:00\",\n            \"prior_value\": \"2022-02-05T23:39:37-05:00\",\n            \"total_active\": 1,\n            \"total_all\": 4,\n            \"sorted_active\": \"[[\\\"92cbfc57-6006-36ab-8ad1-5ea30263d9fc\\\", {\\\"alarmLabel\\\": null, \\\"alarmTime\\\": 0, \\\"assigner\\\": null, \\\"automationId\\\": null, \\\"createdDate\\\": 1644122041127, \\\"deferredAtTime\\\": null, \\\"deliveryType\\\": null, \\\"deviceName\\\": null, \\\"deviceSerialNumber\\\": \\\"G0911B0592850CN1\\\", \\\"deviceType\\\": \\\"A4ZP7ZC4PI6TO\\\", \\\"extensibleAttribute\\\": null, \\\"geoLocationTriggerData\\\": null, \\\"id\\\": \\\"A4ZP7ZC4PI6TO-G0911B0592850CN1-92cbfc57-6006-36ab-8ad1-5ea30263d9fc\\\", \\\"lastOccurrenceTimeInMilli\\\": 0, \\\"lastUpdatedDate\\\": 0, \\\"loopCount\\\": null, \\\"mrmGroupContext\\\": null, \\\"musicAlarmId\\\": null, \\\"musicEntity\\\": null, \\\"notificationIndex\\\": \\\"92cbfc57-6006-36ab-8ad1-5ea30263d9fc\\\", \\\"originalDate\\\": null, \\\"originalDurationInMillis\\\": 335000, \\\"originalTime\\\": null, \\\"personProfile\\\": null, \\\"primaryDeviceState\\\": null, \\\"provider\\\": null, \\\"rRuleData\\\": null, \\\"recurrenceEligibility\\\": false, \\\"recurringPattern\\\": null, \\\"remainingTime\\\": 333000.0, \\\"reminderAppLabel\\\": null, \\\"reminderLabel\\\": null, \\\"reminderSubLabel\\\": null, \\\"skillInfo\\\": null, \\\"sleepTrackingAlarmProperties\\\":1,\\\"another\\\":\\\"qwertyuiop\\\"}]]\",\n            \"sorted_all\": \"[[\\\"956d96dd-f86a-35ea-9417-d8e7df937035\\\", {\\\"alarmLabel\\\": null, \\\"alarmTime\\\": 0, \\\"assigner\\\": null, \\\"automationId\\\": null, \\\"createdDate\\\": 1644119027334, \\\"deferredAtTime\\\": null, \\\"deliveryType\\\": null, \\\"deviceName\\\": null, \\\"deviceSerialNumber\\\": \\\"G0911B0592850CN1\\\", \\\"deviceType\\\": \\\"A4ZP7ZC4PI6TO\\\", \\\"extensibleAttribute\\\": null, \\\"geoLocationTriggerData\\\": null, \\\"id\\\": \\\"A4ZP7ZC4PI6TO-G0911B0592850CN1-956d96dd-f86a-35ea-9417-d8e7df937035\\\", \\\"lastOccurrenceTimeInMilli\\\": 0, \\\"lastUpdatedDate\\\": 0, \\\"loopCount\\\": null, \\\"mrmGroupContext\\\": null, \\\"musicAlarmId\\\": null, \\\"musicEntity\\\": null, \\\"notificationIndex\\\": \\\"956d96dd-f86a-35ea-9417-d8e7df937035\\\", \\\"originalDate\\\": null, \\\"originalDurationInMillis\\\": 900000, \\\"originalTime\\\": null, \\\"personProfile\\\": null, \\\"primaryDeviceState\\\": null, \\\"provider\\\": null, \\\"rRuleData\\\": null, \\\"recurrenceEligibility\\\": false, \\\"recurringPattern\\\": null, \\\"remainingTime\\\": 0, \\\"reminderAppLabel\\\": null, \\\"reminderLabel\\\": null, \\\"reminderSubLabel\\\": null, \\\"skillInfo\\\": null, \\\"sleepTrackingAlarmProperties\\\":1,\\\"another\\\":\\\"qwertyuiop\\\"}]]\",\n            \"status\": \"ON\",\n            \"dismissed\": \"2022-02-05T23:34:03.075791-05:00\",\n            \"device_class\": \"timestamp\",\n            \"icon\": \"mdi:timer-outline\",\n            \"friendly_name\": \"Stephen next Timer\"\n        },\n        \"last_changed\": \"2022-02-06T04:34:03.077149+00:00\",\n        \"last_updated\": \"2022-02-06T04:34:03.077149+00:00\",\n        \"context\": {\n            \"id\": \"edfadd86d60b2adce06ef67f1117d5d2\",\n            \"parent_id\": null,\n            \"user_id\": null\n        },\n        \"timeSinceChanged\": \"12 seconds ago\",\n        \"timeSinceChangedMs\": 11809,\n        \"original_state\": \"2022-02-05T23:39:36-05:00\"\n    },\n    \"_msgid\": \"939d383111eeaa6e\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":270,"y":4080,"wires":[["d07b8b00.3377b","f65facb9.70d12"]]},{"id":"d07b8b00.3377b","type":"function","z":"c791cbc0.84f648","name":"","func":"msg.payload = JSON.parse(msg.data.attributes.sorted_active)[0][1].originalDurationInMillis;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":220,"y":4220,"wires":[["70155594.e05bac"]]},{"id":"f65facb9.70d12","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":510,"y":4080,"wires":[]},{"id":"70155594.e05bac","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":4220,"wires":[]}]

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.