Issue with processing JSON

I am trying to replicate the flow explained here.

At step 2. Ask ChatGPT produced a valid JSON:

{
  "title": "10 Smart Strategies to Economize on Your House Construction",
  "outline": [
    "Embrace Minimalist Design Principles for Cost-Effectiveness",
    "Optimize Material Selection for Affordability and Durability",
    "Harness DIY Building Tactics to Cut Down Labor Costs",
    "Incorporate Reclaimed and Recycled Materials for Unique Savings",
    "Select Energy-Efficient Features for Long-Term Savings"
  ]
}

At step 3 though I am getting “Unexpected token ` in JSON at position 0”

Typically it happens when trying to parse JSON but the string being parsed is not in correct JSON format.

Anyone else dealt with the same problem?

I attempted your exact input and encountered no issues whatsoever. Which code are you using in the code block?

Here was my configuration:

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

with openai designated as the variable for the output from the previous step.

https://cloud.activepieces.com/templates/3XZixHii4oJtqe6pULSKl

Hi @Peter_V

I see the confusion, I think creating extract structured data action that would helps us a lot.

In addition to @Dennis suggestion, we have now JSON piece instead of using code

Thank you,
Mo.

Dennis, I tried the code you suggested, but still getting the same error (“Unexpected token ` in JSON at position 0”).

I am using the template provided by ActivePieces. It produces an error in both cases (with the original code or with the code you provided).

Here’s what I have at step 2

Thanks,
Peter

I wasn’t allowed to attach two screenshots in one post, so here is what I have at Step 3

I did open the link you sent me and I can see that there is a difference between the test JSON output that you generated and the standard OpenAI JSON output that is produced… see below


p

I think I found the issue.

Originally I was using gpt-4.X.

I switched to gpt-3.5 and now OpenAI’s JSON output does not contain the additional text at the beginning and the end of the output.

1 Like

yes, that is exactly the issue. Easiest way to solve it would be to change the prompt a bit so it’ll only output the json, without anything else.

edit: We were writing a reply at the same time I see, glad to see it’s working now!

@abuaboud @Dennis question on the json piece and code… I have noticed that Gemini at times throws the json text in the output, which then causes the same error. Tried structured json output and gave example in prompt. 80%-90% of the time it works, but then I have to change the prompt then save, and then remove the change I did and re-save. Works great again for a bit… have not really dealt with openai on my stuff, but curious if there is different methods to approach this based on the AI?

Thanks