Error message "Unexpected token u in JSON at postion 0"

A while a go I started a flow. Now I want to finish it. The webhook gets the correct data. But how do I parse the data so that I can insert it into Google Sheets? I found some code here on the forum:

export const code = async (inputs) => {
  return JSON.parse(inputs.text);
};

But the result is an error message “Unexpected token u in JSON at position 0”. What do I need to change to get the correct data?

Error message:

Data:

The JSON I have is not an array. I think that the code is for an array, correct? I don’t have much control over how the JSON is created.

Hello @beatrix.willius, please check your code step inputs, apparently you didn’t put an input with the name “text”.

Yes, I probably should use input.body and not inputs.text:

export const code = async (inputs) => {
  try {
    // Ensure inputs.body is a string and parse it
    if (typeof inputs.body === 'string' && inputs.body.trim()) {
      return JSON.parse(inputs.body);  // Parse the body as JSON
    } else {
      throw new Error('Body is not a valid JSON string.');
    }
  } catch (error) {
    return { error: error.message }; 
  }
};

But now I broke ActivePieces:

What is the timeout? The flow has been running for a couple of minutes now.

The flow finally finished and I got an error: {

1 item

error:

“Body is not a valid JSON string.”

}

Any idea how to solve this problem?

Some success. I removed the code and the loop. I now don’t get an error anymore. But the new row in the Google Sheet is empty. Any idea how to get the data?


Majorly frustrated…

https://cloud.activepieces.com/templates/qvl8fDDGTiWnJHiQRw9fL
I created a template as you can see above :arrow_heading_up:

In that template I get text content from the webhook, this is the body of the webhook:

{
    "content": "[{\"A\":\"1\",\"B\":\"2\"},{\"A\":\"1\",\"B\":\"2\"}]"
}

an array of two items, and I want to insert them to google sheets, so I parse the content in the code step:


then I insert them into google sheets in one “Insert Multiple Rows” action:

and that should be it.

And sorry for not responding earlier, I forgot to check back for replies on this post.