Extract JSON from Image using AI Agent

Hi all its me again.

Trying to get the contents from an ai agent to be in JSON format but it keeps having a “content” parent. How can i get the actual content in JSON format?

Hey @Smigao

You can have a set node after with the following

{{ JSON.parse($json.content) }}

Not sure im getting it right. seems like it still has the parent:

@Smigao

What about {{ $json.content[0] }} ?

Could you tell us more about how having a ‘content’ field is problematic for you? Why are you trying to get rid of it (you likely won’t).

I ended up using a javascript in a function code node to remove the parent.

return items.map(item => {
  let raw = item.json.content;

  // Step 1: Clean markdown code fences
  raw = raw.replace(/```json|```/g, '').trim();

  try {
    // Step 2: Parse safely
    const parsed = JSON.parse(raw);
    return { json: parsed };
  } catch (err) {
    // Step 3: If it fails, return the error and original input
    return {
      json: {
        error: true,
        message: 'Failed to parse JSON from content',
        details: err.message,
        original: raw,
      }
    };
  }
});

2 Likes

I’m very new to this + coding - I want to automate adding results to a spreadsheet, I assumed that the json formatting might not upload correctly to the spreadsheet.

Ive made some progress. My other comment has a javascript that removes the “content” parent.

In the place where you wanted use it, you could just extract the value of the content with {{ $json.content }} and the content wouldn’t be there.

Yes that was going to be my final suggestion but didn’t want to give you code right away.

Nice one

1 Like