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
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 }}”
}
}
]
}
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:
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
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.