Can not validate as json

Describe the problem/error/question

My workflow is basically reading a txt file and then convert it to json but it is stuck at Edit Field2 node, it won’t validate as a proper json. I already have escaped double quotes & linebreaks but I don’t know what is making it invalid. For some odd reason if I remove the Read from Disk node and put my data into a Edit Field Node it will then validate as a json

This is what’s inside summary.txt :

[CENTER][IMG]https://i.postimg.cc/jdkhHwXp/309192.jpg[/IMG][/CENTER]
[B]Synopsis[/B]
Not even a hero, Takumi Iruma gets accidentally mixed in with a group of heroes chosen to be summoned to another world. As compensation for the mix-up, a goddess offers him the right to choose any skill he wishes for! Hoping for a peaceful and quiet life that has nothing to do with fighting or going into battle, he chooses a seemingly boring creation skill. However, it turns out "alchemy" is the most powerful skill that allows him to create everything from a holy sword to flying ships! This cheat skill he unexpectedly acquired turns him into a wealthy merchant and makes him undefeatable in battles! A heartwarming adventure story about (possibly) the most powerful alchemist in another world!

[B]Alternative Titles[/B]
Synonyms: Someday Will I Be the Greatest Alchemist?
Japanese: いずれ最強の錬金術師?

[B]Information[/B]
Episodes: Unknown
Broadcast: Wednesdays at 22:00 (JST)
Producers: Pony Canyon, Jinnan Studio
Licensors: None found
Studios: Studio Comet
Genres: Adventure, Fantasy
Theme: Isekai
Duration: 23 min.
MAL: [URL]https://myanimelist.net/anime/58822[/URL]

[B]PV[/B]
[MEDIA=youtube]-qZJ9dD7KlU[/MEDIA]

[boxinfo]File Information[/boxinfo]
Group: SubsPlease
Video: 720p = 1280x720 | 1080p = 1920x1080 | AVC 23.976 FPS
Audio: Japanese AAC
Subtitles: English ASS
Container: MKV

What is the error message (if any)?

The ‘JSON Output’ in item 0 contains invalid JSON

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • **n8n version:**1.81.4
  • **Database (default: SQLite):**default
  • **n8n EXECUTIONS_PROCESS setting (default: own, main):**default
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):**npm
  • **Operating system:**Windows Server 2022

You may also need to escape other control characters like e.g. backslashes or something that may appear in other texts you receive in the future.

How about using the Code node to create an output object and let n8n to convert it to a properly formed json or using JSON.stringify() against the text?

If this helps to resolve your issue, please mark this post as a Solution.

can you provide an example workflow?

Hi @Ruriko
I think @Olek is correct,
you can use JSON.stringify()

simply it will be like this:

"Value": {{ JSON.stringify($json.data.replace(/\\n/g, '<br>').replaceAll(/\"/g, '\\\\\"') )}},

here is working example:

Here I added two options on top of your original Set node.

Try both and pick the one that works for you. I’d vote for the Code node approach for the flexibility it offers, in this particular case.

I retained the replacement of newlines with <br/> HTML tag, but you may not need this if the targeted platform handles markdown properly.

BTW, @mohamed3nan I think /\\n/g will go for literally \n string, not for a newline character

If you want to transform the entire text (which looks like markdown to me) into HTML yoy may want to employ Markdown | n8n Docs node after Extract from file.

If this resolves your question, please like :heart: this post and mark it as a :white_check_mark: Solution.

2 Likes

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