Is there a way to remove the extra characters from JSON result?

Hi,
I’m using the OpenAI output as JSON, however it being wrapped in comments that are not recognised by the parser.
Is there any way to avoid this without too many loops with trimming the text?

At the end I want to split the result into different columns, like below

Are you referring to the backticks?
What does your prompt look like?
You should be able to craft it so that only the JSON is returned.

1 Like

This is not a JSON format… how did you get it? What kind of script?.. You can clean it by replacing the first characters… anything before the first curly bracket must go away…

yes, these backticks are causing me an issue.

I’m using output from OpenAi integration with the following prompt (part is shown)
image

There is also an option to return in JSON, which makes no difference in this case.
image

Replace your code with this one:

if(p1){
let inputString = p1;
let indexOfFirstCurlyBracket = inputString.indexOf('{');

if (indexOfFirstCurlyBracket !== -1) {
  let resultString = inputString.substring(indexOfFirstCurlyBracket);

return (JSON.parse(resultString).json.Title);
} else {
 return "Curly bracket not found in the string."
}
}
return

Thank you for suggestion. I tried this code, but it did not work in the JS column. However it gave me an idea to construct a different code which did work at the end without adding interim columns

1 Like

This is to remove the invalid char at the start
How to remove invalid chars at the start before { as well as after }

This is a very old thread. Can you please explain more about your use case?
Please show the data you have, how you get it, and your desired end ressult.

From the AI response we will get json like below
“Here is the sample response{“productId”: 123, “productCode”:“code1”} end”
Need to remove prefix text “Here is the sample response” as well as suffix text at end after last } which is “end”

Change your prompt. Instruct the AI to return only the JSON and nothing else.

Question: Are you using “Document to Text” or “Generate Text”? If so, try using “Text to JSON” instead.

Yes but sometimes the prompt return invalid text at start and end if promptInput is not proper
The prompt input is unstructured like “123 code1” then it returns output in json
{“productId”: 123, “productCode”:“code1”}

You should use Text to JSON to ensure the JSON output.