Extract value from array based JSON

Hi,
I have an json payload where data is organised inside an array and I want to obtain a value from one of the positions. since it is inside an array I need to filter the position that I want using a predicate. But I didn't find a working solution for this.

I am sending an JSON example below. I was trying to use a change node with a path like msg.payload.consumption[measurementType="total-consumption"].wNow but it doesn't work.

Any suggestions on how this could be achived?

JSON:
{

"consumption": [
    {
        "type": "eim",
        "activeCount": 1,
        "measurementType": "total-consumption",
        "readingTime": 1623014333,
        "wNow": 471.878,
        "whLifetime": 64459558.361,
        "varhLeadLifetime": 22337648.955,
        "varhLagLifetime": 12438387.289,
        "vahLifetime": 67290585.179,
        "rmsCurrent": 6.355,
        "rmsVoltage": 688.198,
        "reactPwr": -1234.527,
        "apprntPwr": 4373.194,
        "pwrFactor": 0.11,
        "whToday": 28270.361,
        "whLastSevenDays": 216088.361,
        "vahToday": 32730.179,
        "varhLeadToday": 13200.955,
        "varhLagToday": 7067.289
    },
    {
        "type": "eim",
        "activeCount": 1,
        "measurementType": "net-consumption",
        "readingTime": 1623014333,
        "wNow": 476.109,
        "whLifetime": 43014212.263,
        "varhLeadLifetime": 22337561.058,
        "varhLagLifetime": 6528732.71,
        "vahLifetime": 67290585.179,
        "rmsCurrent": 5.533,
        "rmsVoltage": 688.309,
        "reactPwr": -1073.666,
        "apprntPwr": 1267.139,
        "pwrFactor": 0.38,
        "whToday": 0,
        "whLastSevenDays": 0,
        "vahToday": 0,
        "varhLeadToday": 0,
        "varhLagToday": 0
}
]

}

Thanks in advance,
Norberto

You need to select JSONata expression

[{"id":"5bae521e.35b5dc","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"consumption\": [     {         \"type\": \"eim\",         \"activeCount\": 1,         \"measurementType\": \"total-consumption\",         \"readingTime\": 1623014333,         \"wNow\": 471.878,         \"whLifetime\": 64459558.361,         \"varhLeadLifetime\": 22337648.955,         \"varhLagLifetime\": 12438387.289,         \"vahLifetime\": 67290585.179,         \"rmsCurrent\": 6.355,         \"rmsVoltage\": 688.198,         \"reactPwr\": -1234.527,         \"apprntPwr\": 4373.194,         \"pwrFactor\": 0.11,         \"whToday\": 28270.361,         \"whLastSevenDays\": 216088.361,         \"vahToday\": 32730.179,         \"varhLeadToday\": 13200.955,         \"varhLagToday\": 7067.289     },     {         \"type\": \"eim\",         \"activeCount\": 1,         \"measurementType\": \"net-consumption\",         \"readingTime\": 1623014333,         \"wNow\": 476.109,         \"whLifetime\": 43014212.263,         \"varhLeadLifetime\": 22337561.058,         \"varhLagLifetime\": 6528732.71,         \"vahLifetime\": 67290585.179,         \"rmsCurrent\": 5.533,         \"rmsVoltage\": 688.309,         \"reactPwr\": -1073.666,         \"apprntPwr\": 1267.139,         \"pwrFactor\": 0.38,         \"whToday\": 0,         \"whLastSevenDays\": 0,         \"vahToday\": 0,         \"varhLeadToday\": 0,         \"varhLagToday\": 0 } ]}","payloadType":"json","x":120,"y":2260,"wires":[["cce946dc.3de4b8"]]},{"id":"cce946dc.3de4b8","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.consumption[measurementType=\"total-consumption\"].wNow","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":2260,"wires":[["404ab792.b606f"]]},{"id":"404ab792.b606f","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":2280,"wires":[]}]

Here is an example

In a function node...

var x = msg.payload.consumption.find(e=>e.measurementType=="total-consumption");
msg.payload = x;

Thanks for your help guys.
I have ended up doing the function.
Norberto

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