Format for JSON parameter need to be an valid JSON?

Any insight would be helpful. It’s sucha simple line of code and I can’t figure it out.
I’ve gone through articles and discussions and I still can’t figure out what is wrong with the code.
I’ve tried:

{
  "options": {{ $json["values"] }}
}
{
  "options": {{ JSON.stringify($json["values"]) }}
}
{
  "options": {{ $json["values"].map(item => JSON.stringify(item)).join(', ') }}
}

Describe the problem/error/question

I keep JSON parameter need to be an valid JSON

What is the error message (if any)?

JSON parameter need to be an valid JSON

Please share your workflow



Share the output returned by the last node

Information on your n8n setup

  • n8n version:
    Can’t tell there was an upgrade a day or two ago and than another upgrade but the system wont let me upgrade (it logs out)
  • Database (default: SQLite):
    ??
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
    ??
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
    Cloud
  • Operating system:
    MacOS

Hi @communitywork

Could you possibly share a screenshot or the error that you are getting in your HTTP node? And also the configs you have set up there.

You can also find out which version of n8n you are on by checking the About n8n tab from your panel on the left
image

Thanks for reaching out @ria

I’m on 1.51.1 - I updated last night.

This morning ran the node to provide the information below and I got a new error message. I was trying this for a couple of days and kept getting the JSON parameter. Perhaps it was the update.

This morning it looks like this:

So, I updated the code and it’s back to the JSON parameter message.
Any ideas?

@communitywork , I can see a few issues with your configuration of the HTTP Request node which results in the error “invalid JSON” in the last screenshot.

  1. You seem to have forgotten to enable “expression” mode for JSON text-area field. When enabled, the outcome is typically displayed just below the text-area field, which is not the case in the screenshot
  2. The expression itself breaks the evaluation and hence results in the invalid JSON

It is not clear to me what the content of the body you want to build as the expression you currently have does not make much sense to me. You are using the spread syntax, ...i while the iterable object is an array of strings.

If I leave that logic fixing the actual syntax issue, the outcome would be is not likely what you expect.

Assuming the values property (inspired by the previous screenshot) is

{
  "values": [
    "abc",
    "def"
  ]
}

and the corrected expression syntax (to ensure JSON is properly formed) is

{{
{
  "options":  $json.values.map(
    i => {return {"type": "TEXT_NUMBER", ...i\}})
}
}}

The outcome will be

{
  "options": [
    {
      "0": "a",
      "1": "b",
      "2": "c",
      "type": "TEXT_NUMBER"
    },
    {
      "0": "d",
      "1": "e",
      "2": "f",
      "type": "TEXT_NUMBER"
    }
  ]
}

I’m pretty sure that is not your goal.

Perhaps you need the body to look like this?

{
  "options": [
    "abc",
    "def"
  ],
  "type": "TEXT_NUMBER"
}

In that case the JSON body expression will be just

{{
{
  "options":  $json.values,
  "type": "TEXT_NUMBER"
}
}}

If you let us know what the body in the POST request should look like we can share the proper expression to produce it.

PS. In the correction of your version of the expression I had to change a few things to produce valid JSON

$json.values.map(
    i => {return {"type": "TEXT_NUMBER", ...i\}})

, namely

  1. introduced return to return the object
  2. escaped the closing curly bracket (\}) as without the escape character the expression gets broken
3 Likes

Hello community, I’m having the same problem but with a much simpler json. I’ve seen that there are some questions in the forum around the same issue, but couldn’t find a solution that works for me.

Here is my node; syntactically n8n doesn’t complain on anything but on every run it says “JSON parameter need to be an valid JSON” . I tried:

  • removing the dynamic variable
  • using specify body via fields.

But still no progress, do you guys see anything obvious? I appreciate upfront the help.

@Claudson_Oliveira , there is no need in using JSON.stringify() the way you did. It makes no difference. It is clear that the problem is with the value of $json.output as there is no other spot that could invalidate JSON. If you replace $json.output.replace(/[\n"\&\r\t\b\f]/g, ' ') with a regular string (like "abc"), it should work proving my statement.

You could try applying JSON.stringify() to that string rather than the whole JSON like this, JSON.stringify($json.output.replace(/[\n"\&\r\t\b\f]/gm ' ')). Note that I also added m modifier to your RegEx, which means applying the pattern to any part of the string separated by a new line.

I can only guess that the offending string includes some characters (either visible or not) that prevent validating JSON. You seem to be trying to replace some such characters. Could you include the previous node that holds the actual string, please? It will be easier to help you out if nothing suggested so far still works.

First of all thank you for your time ihortom.
Here is part of the flow including the node before the request

I just made the test of removing the $json.output and I still have the problem :sweat_smile: , which might indicate that my whole json is wrong or with a hidden character.

I will also provide the version before my test, which includes the original text with some breaklines that I try to remove with the replace call.

@Claudson_Oliveira , everything works as expected for me. The problem you are reporting seems to be somewhere else. Try running your “elevenlabs_generate_audio” HTTP Request node on its own to verify that.

Here’s my try

Fixed JSON

Composed JSON

Test workflow

I suspect that your handling of the loop could be the cause.

Thank you again, I think indeed it was correct. I recreated the node from the scratch and it worked. Initially I had created it form a curl statement and I think something went wrong in the request payload, as you mentioned previously.

After recreating the node everything worked fine.

Thank you @ihortom!
I will try your corrections and suggestions as soon as I fix a preceding node that broke.

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