Extract data from Json not fixed position

Describe the problem/error/question

Hello there,

I get data from a json where there are tons of information.
Right now I just get the data I need using the position of the information needed.

Like this:
{{$node["Consulta API Victron"].json["records"][167]["rawValue"]}}

That works well … until the position change and is not 167 anymore and the node fails.

What would be the way to get a value from the json by a key other than the position.

The json is like this:

[
{
"success": true,
"records": [
{
"idSite": 111111,
"timestamp": 1701917871,
"Device": "Gateway",
"instance": 0,
"idDataAttribute": 1,
"description": "gatewayID",
"formatWithUnit": "%s",
"dbusServiceType": null,
"dbusPath": null,
"code": "d",
"bitmask": 0,
"formattedValue": "Venus OS",
"rawValue": 2,
"dataAttributeEnumValues": [
{
"nameEnum": "VGR, VGR2 or VER",
"valueEnum": 0
},
{
"nameEnum": "Venus OS",
"valueEnum": 1
},
{
"nameEnum": "Venus OS",
"valueEnum": 2
},
{
"nameEnum": "Coupling / GlobalLink",
"valueEnum": 3
},
{
"nameEnum": "PHYSEE NodeMCU",
"valueEnum": 4
}
],
"id": 1
},
{
"idSite": 111111,
"timestamp": 1701917871,
"Device": "Gateway",
"instance": 0,
"idDataAttribute": 386,
"description": "Productid",
"formatWithUnit": "%s",
"dbusServiceType": null,
"dbusPath": null,
"code": "mi",
"bitmask": 0,
"formattedValue": "C002",
"rawValue": "C002",
"id": 2
},
{
"idSite": 111111,
"timestamp": 1701917871,
"Device": "Gateway",
"instance": 0,
"idDataAttribute": 387,
"description": "MachineName",
"formatWithUnit": "%s",
"dbusServiceType": null,
"dbusPath": null,
"code": "mn",
"bitmask": 0,
"formattedValue": "Venus GX",
"rawValue": "Venus GX",
"id": 3
},

I would like to get the value (for example) of "description": "gatewayID",

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 0.189.1
  • Database: SQLite
  • n8n EXECUTIONS_PROCESS setting: default
  • Running n8n via npm
  • Operating system: Linux

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @RichiWalichi,

If you have a value that is fixed that you know so in your case you know the description will contain Gateway you could use a code node to loop over the array or use a Split Out node to convert the array to items then use an If node, Swtich node or something else to find the values you are after like the example below.

1 Like

Or use Query JSON with JMESPath | n8n Docs

1 Like

Many thanks @pemontto and @Jon, thanks to your help I’ve able to get the value.

Eventually I’ve use JMESPath with this value:

let battery = $jmespath($json.records, "[?description=='State of charge'].rawValue" )
return {battery};

Search in the Json the label description and if it has “State of charge” then get the rawValue.

Many thanks for the help :wink:

2 Likes

I always forget about JMESPath, I still don’t think I have used it :slight_smile: This seems like a great use for it.

1 Like

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