The 'JSON Output' in item 0 contains invalid JSON

I am trying to create a JSON output based on the following JavaScript however I receive the following error message
ERROR: The ‘JSON Output’ in item 0 contains invalid JSON

{ “revisedAt”: 2024-02-“07T16”:“15”:55Z, “items”: [ { “productId”: “supplier-ec06e52d-582f-4240-af40-fa1a4fbaf382-193”, “discountedPrice”: { “currency”: “GBP”, “amount”: “9.08” } } ] }; Original input: { “revisedAt”: 2024-02-07T16:15:55Z, “items”: [ { “productId”: “supplier-ec06e52d-582f-4240-af40-fa1a4fbaf382-193”, “discountedPrice”: { “currency”: “GBP”, “amount”: “9.08” } } ] }

This is the code I am using which seems fine but doesn’t give a good output. Could you please help me on that
{
“revisedAt”: {{ new Date().toISOString().slice(0,19)+‘Z’}},
“items”: [
{
“productId”: “{{ $json.id }}”,
“discountedPrice”: {
“currency”: “GBP”,
“amount”: “{{ $json.amount }}”
}
}
]
}

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:

That json is pretty funky, where did you copy that from? It is definitely not valid json.

I cleaned it up to this:

{
  "revisedAt": "2024-02-07T16:15:55Z",
  "items": [
    {
      "productId": "supplier-ec06e52d-582f-4240-af40-fa1a4fbaf382-193",
      "discountedPrice": {
        "currency": "GBP",
        "amount": "9.08"
      }
    }
  ]
}

Even with valid json, you also can’t just do {{ $json.id }} you’ll need to do {{ $json.items[0] }} first, since it’s nested in a list called items.

You can tell it’s a list/array because it’s in [ ]. You can’t query arrays the same as you are used to with json, you need get it by index AKA where it is in the list.

Saying items[0] is saying to return the first item in the list (since lists/arrays start at 0 in JS). If you wanted to get the 2nd item (which doesn’t exist in this case) you would put items[1]. Then since it’s a json object in the array, you can query it again with .discountedPrice.amount.

So your expression would look like {{ $json.items[0].discountedPrice.amount }}

You could of course also just drag it from the input section:

Screen Recording 2024-02-07 at 12.50.15 PM

Also thought i’d add if there is a case where the are multiple items in the list you can find it by value using the find() method inline like this instead of using the index

{{  $json.items.find((id) => "supplier-ec06e52d-582f-4240-af40-fa1a4fbaf382-193").discountedPrice.amount }}

of course just replace the string value i put in there for the actual search value you would use. The id variable in the find method can be changed to anything it doesn’t matter.

1 Like

For some reason the edit button seems to be missing :thinking: so im replying instread

I wanted to correct a mistake, to search through the json you would format it like this:

{{ $json.items.find((items) => items.productId === "supplier-ec06e52d-582f-4240-af40-fa1a4fbaf382-193").discountedPrice.amount }}

The other way would have matched for the first item regardless because it didn’t have a comparison.