# Format for JSON parameter need to be an valid JSON?

**URL:** <https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713>\
**Category:** Questions\
**Created:** [July 25, 2024, 12:45am UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713 "2024-07-25T00:45:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![communitywork](https://avatars.discourse-cdn.com/v4/letter/c/c4cdca/32.png) [@communitywork](https://community.n8n.io/u/communitywork)\
**Post date:** [July 25, 2024, 12:45am UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/1 "2024-07-25T00:45:22Z")

</div>

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:

```auto
{
  "options": {{ $json["values"] }}
}

```

```auto
{
  "options": {{ JSON.stringify($json["values"]) }}
}

```

```auto
{
  "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

 ![Screenshot 2024-07-24 at 18.35.43](https://europe1.discourse-cdn.com/n8n/original/3X/e/4/e43e82a84a80ed405b8aea6a2f4d67b3b6b52863.jpeg)  
 ![Names](https://europe1.discourse-cdn.com/n8n/original/3X/5/3/53624446654fb4560dfaba95134672e27a10d2e3.jpeg)  
 ![Wrokflow](https://europe1.discourse-cdn.com/n8n/original/3X/0/9/0966e9025cfa7ad63295b93861a12247ca5eb9ef.jpeg)

## 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

---

<div class="post-metadata">

**Author:** ![ria](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ria/32/99251_2.png) [@ria](https://community.n8n.io/u/ria)\
**Post date:** [July 25, 2024, 10:27am UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/2 "2024-07-25T10:27:25Z")

</div>

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](https://europe1.discourse-cdn.com/n8n/original/3X/6/9/69abc227593d5981b7e35ab3f32fdc45b67aa68e.png)

---

<div class="post-metadata">

**Author:** ![communitywork](https://avatars.discourse-cdn.com/v4/letter/c/c4cdca/32.png) [@communitywork](https://community.n8n.io/u/communitywork)\
**Post date:** [July 25, 2024, 1:54pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/3 "2024-07-25T13:54:36Z")

</div>

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:

 ![Screenshot 2024-07-25 at 07.43.14](https://europe1.discourse-cdn.com/n8n/original/3X/2/3/236a62528056e97c6853f59f56bd0d2d40fc52fa.jpeg)

 ![Screenshot 2024-07-25 at 07.33.14](https://europe1.discourse-cdn.com/n8n/original/3X/d/5/d590636d585f9b0504a0f9aef92538556c058f5d.jpeg)

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

 ![Screenshot 2024-07-25 at 07.52.49](https://europe1.discourse-cdn.com/n8n/original/3X/5/f/5fc920fb14c7e2fa94084c4a2aba17c4a0966a05.jpeg)

---

<div class="post-metadata">

**Author:** ![ihortom](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ihortom/32/11473_2.png) [@ihortom](https://community.n8n.io/u/ihortom)\
**Post date:** [July 30, 2024, 5:02pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/4 "2024-07-30T17:02:29Z")

</div>

@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

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

```

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

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

```

The outcome will be

```auto
{
  "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?

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

```

In that case the JSON body expression will be just

```auto
{{
{
  "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

```auto
$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

---

<div class="post-metadata">

**Author:** ![Claudson\_Oliveira](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/claudson_oliveira/32/42333_2.png) [@Claudson\_Oliveira](https://community.n8n.io/u/Claudson_Oliveira)\
**Post date:** [August 29, 2024, 11:49am UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/5 "2024-08-29T11:49:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ihortom](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ihortom/32/11473_2.png) [@ihortom](https://community.n8n.io/u/ihortom)\
**Post date:** [August 29, 2024, 4:06pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/6 "2024-08-29T16:06:48Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Claudson\_Oliveira](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/claudson_oliveira/32/42333_2.png) [@Claudson\_Oliveira](https://community.n8n.io/u/Claudson_Oliveira)\
**Post date:** [August 29, 2024, 4:48pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/7 "2024-08-29T16:48:05Z")

</div>

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 😅 , which might indicate that my whole json is wrong or with a hidden character.

---

<div class="post-metadata">

**Author:** ![Claudson\_Oliveira](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/claudson_oliveira/32/42333_2.png) [@Claudson\_Oliveira](https://community.n8n.io/u/Claudson_Oliveira)\
**Post date:** [August 29, 2024, 4:54pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/8 "2024-08-29T16:54:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ihortom](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ihortom/32/11473_2.png) [@ihortom](https://community.n8n.io/u/ihortom)\
**Post date:** [August 31, 2024, 8:40pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/9 "2024-08-31T20:40:18Z")

</div>

@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_

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/c/5/c5a14a9fac36f4f4f1c45f455447d9c887d2a13e.png)

_Composed JSON_

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/1/3/13b03efb486f31fabbffad8446548d06322b28c7.png)

_Test workflow_

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

---

<div class="post-metadata">

**Author:** ![Claudson\_Oliveira](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/claudson_oliveira/32/42333_2.png) [@Claudson\_Oliveira](https://community.n8n.io/u/Claudson_Oliveira)\
**Post date:** [August 31, 2024, 11:44pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/10 "2024-08-31T23:44:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![communitywork](https://avatars.discourse-cdn.com/v4/letter/c/c4cdca/32.png) [@communitywork](https://community.n8n.io/u/communitywork)\
**Post date:** [October 7, 2024, 6:36pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/11 "2024-10-07T18:36:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [January 5, 2025, 6:37pm UTC](https://community.n8n.io/t/format-for-json-parameter-need-to-be-an-valid-json/50713/12 "2025-01-05T18:37:45Z")

</div>

Closed on January 5, 2025, 6:37pm UTC

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