Get value of an item from a Json array by ID and not by order

Hello everyone,

I’m trying to get a data from a json, and while it works for me most of the time, I think I’m not doing it right.

I am receiving a json with several items, like this:

image

For my part I want to extract the value of node 3, and I am using this expression:

{{$node[“GetCF”].json[“customFieldItems”][3][“value”][“text”]}}

and it works very well, however the problem is when the array does not have node 2 (because when there is no data in that field, the json does not include it), then node 3 becomes 2 and all the following ones go back one number.

I would like to get the value with the ID, but I don’t know how to get it.

Could you please help me.
Thank you very much!

Hey @patomi, I have described a possible approach in a previous post which you can find here:

The field names are slightly different, but you should be able to adjust the code accordingly. Let me know if you run into any trouble with this (it would be great if you could copy the JSON version of your item in that case).

Yesterday’s version 0.167.0 of n8n also added support for JMESPath in n8n, which should simplify many queries like this (but I haven’t used it yet, so don’t have an example ready unfortunately).

Great!..it worked perfectly.

Thanks so much for the help.

I copy here the json and the nodes.

JSON

[
  {
    "id": "622f30ed62b9be77a6ba5890",
    "name": "titulo tarjeta",
    "customFieldItems": [
      {
        "id": "622f30ee6f9abf7a7e0494c2",
        "value": {
          "text": "+55555555551"
        },
        "idCustomField": "60081df737fe77290115029b",
        "idModel": "622f30ed62b9be77a6ba5890",
        "modelType": "card"
      },
      {
        "id": "622f30eee731f12efe1de925",
        "value": {
          "text": "Texto personalizado"
        },
        "idCustomField": "60081df737fe77290115029d",
        "idModel": "622f30ed62b9be77a6ba5890",
        "modelType": "card"
      },
      {
        "id": "622f30eebc0aac3c95dcc81a",
        "value": {
          "text": "[email protected]"
        },
        "idCustomField": "60081df737fe77290115029f",
        "idModel": "622f30ed62b9be77a6ba5890",
        "modelType": "card"
      },
      {
        "id": "622f30ee71c74a6d438336d0",
        "value": {
          "text": "54366"
        },
        "idCustomField": "601aa87e0c1a576d64a19957",
        "idModel": "622f30ed62b9be77a6ba5890",
        "modelType": "card"
      },
      {
        "id": "622f30ef6d59828142be1597",
        "value": {
          "text": "XXXXX XXXXXX 2331, vitacura, Región Metropolitana, Chile"
        },
        "idCustomField": "607ae98e6db6ca443c3fe168",
        "idModel": "622f30ed62b9be77a6ba5890",
        "modelType": "card"
      },
      {
        "id": "622f30ef815eb50f016143ab",
        "value": {
          "text": "Cerca del supermercado"
        },
        "idCustomField": "607ae99d673dfa5627c075f8",
        "idModel": "622f30ed62b9be77a6ba5890",
        "modelType": "card"
      }
    ]
  }
]

IN NODE FUNCTION ITEM

item.myText = item.customFieldItems.find(field => field.idCustomField == '60081df737fe77290115029b').value;

return item;

EXPRESION TO OBTAIN ITEM FROM ANOTHER NODE

{{$node["FunctionCFBYID"].json["telefono"]["text"]}}

Once again, thank you very much for your valuable time and help.

1 Like

Excellent, glad to hear this worked and thanks so much for sharing your exact solution!

1 Like