Translate JSON file with Deepl

Hello n8n Community :slight_smile:

My project is to translate a Wordpress elementor JSON file from langage A to langage B and Save the new file on local or Google Drive or just update the original file with the translation.

More details Below :slight_smile:

I create a website with Wordpress to try to translate elementor webpages.
For translation I use Polylang plugin and Deepl API, but Polylang can’t translate automaticallly pages built with elementor builder.

So I decide to automate the translation with n8n by exporting my Wordpress page built with elementor that is in JSON format on my VPS and then make a workflow to read the JSON file and use the Deepl API to translate the content page without hurting the structure. Next I will save the page changes in a new JSON file or just update the original.

So I stardted but now I am totally lost. You can see in my workflow that after the Deepl node, I don’t know what to make. :sweat_smile:

The plan is…
1) => Import in n8n my Json wordpress elementor file

2) => read or extract in the JSON file, the content data to translate ( title of content, content body )

3) => Use Deepl API to translate the content to from langage A to langage B

4) => Use the content translate with Deepl ( title of content, content body ) to update the JSON file with the original structure (The structure of the webpage have to be the same)

5) => Create the new JSON file with translation or update the original.

6) => Save the JSON file on my computer, VPS or Google Drive.

I don’t know if it’s triky and what steps or nodes I miss, but I’m determined to learn and succeed in this challenge. I think it will help a lot of people too.

I don’t know what to do after the Deepl API node. How parse or take all the data content and translate it…

That’s where I’m at, it’s a little light but then I don’t know how to move forward…

Thanks in advance for your help :grinning: :grinning:

My Setup
I use n8n on with cloudron app

  • n8n version: 1.53
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @clovici67 , Deepl returns the data in the form

{
  "detected_source_language": "<LANG>",
  "text": "<TRANSLATED TEXT>"
}

You can merge this data with the original data for clarity and process it further omplementing steps 5 and 6.

Hello
Thanks for reply.

I copy your workflow but I don’t know what to do next … But I try to do my best
I am very newbie on code. Below what I do but. I try this, but got an error when I try to import the new JSON file to Wordpress

The Wordpress error on elementor I got when importing file :grinning:

If someone can help please …

Thanks

Hi @clovici67 ,

I put together this demo workflow that shows one way to approach this.
Let me know, if this helps.

Cheers, Ingo

Hello @Ingo
Really, thanks for the workflow but I don’t understand how I connect my JSON Wordpress file to your new Workflow. The write file from disk and convert to file are isolated to the other nodes… What I have to do ?

Thanks one more time

Hi @clovici67 ,

  1. You remove the first code node on the left - it is just there to inject some sample JSON and to demonstrate it’s working
  2. You connect the file nodes (ā€œImport JSON fileā€) instead and configure them corresponding to your setup
  3. You connect the last node ā€œwrite to diskā€ to the JSON output and configure it approprietly, or replace it with another node - depending on what you want to do with the file

Happy flowgramming!

Thanks Ingo !

I do my best but can’t achieve this because I don’t know how to parse my json file data to extract title, content, etc… In your example, we can see immediatly the data we wan, but in my case, because I use the binary read file, I don’t see in the in the JSON tab the data I will extract. The data are in the binary tab. So I don’t know how to extract this or convert this to JSON clear data like you.

Here Ć  screenvideo who I show you what I got : Screenvideo-n8n

Maybe I can send you my JSON file so you can test and see what I mean ?
Here is the link to download my JSON file : My-JSON-FILE

I’m lost, I don’t know how to use the convert JSON file node.
Help please …

I changed nodes to get my data. Instead of using ā€œconvert to fileā€ I tried ā€œExtract from fileā€ and it seems to work. With this node I can extract the desired information for translation. I don’t know if this is good for the rest or if I should absolutely use ā€œConvert to fileā€. However, in the ā€œupdate translated fields in originalā€ node, I’m having trouble. I don’t know how to map my information here to continue the workflow…

What’s the difference between ā€œconvert to fileā€ and ā€œExtract from fileā€?

Hi @clovici67 ,

you did the right thing - this was a mistake on my behalf. It should always have been the extract node at the start. I didn’t notice as I only tested the main flow in the middle.

image

The extract node takes the binary data (that was read from disk) and converts it into JSON (or any of the other provided formats you want to work with) to proceed with data transformation. The convert node works the other way around - after you’re finished with your transformations, it allows you to convert the data into a binary file.

Cheers, Ingo

thanks. And for the ā€œupdate translated fields in originalā€ node, I’m having trouble. I don’t know how to map my information here to continue the workflow…

You use javascript in your node, how I can do this for my purpose ?

Yes, it’s a bit of JS in the code node and it will have to be adjusted according to your needs.

I would recommend the following:

  • inspect the JavaScript in the code node
  • really try to understand it. If you think you’ve tried, try harder. :wink:
  • ask your friends Claude or ChatGPT to give you a step by step explanation. They understand JavaScript and even n8n and will be great supporters
  • once, you have a good sense of what’s happening, formulate your customization requirements as a prompt
  • share your success with us here, once you’ve figured it out

You can do it! :+1:

Hey Ingo,

Thanks for your time…

I’m really trying, but I just can’t do it! I’ve asked chatgpt to give me some javascript code, but I still get the same errors and I don’t understand why! All my previous nodes are well configured but in the javascript code given by chatgpt I have these errors:

ā€œExpected inputs: 3, receivedā€
ā€œTranslated data (title or content) are missingā€.

Here’s my last script with all the parameters and I’m stuck at the javascript node…

If anyone out there can download my JSON file and try to do the same thing on their end, that would be really great. I’m trying but I’m lost

Hellooooo ! :grinning:
Great News ! I finally success to achieve what i want but just one thing In the final result, the json file generated is not the same as the original so I can’t import them in to Wordpress. And right now I have no idea how to do it.

Thanks to Claude.ai, I’ve success to create the right javacript code to continue my workflow and generate the JSON file with the translations.

Everything’s fine, but the JSON file generated is not compatible with Elementor! So I compared the JSON file generated with n8n and the original Elementor JSON file. And that’s when I noticed a difference…

In the original JSON file, the beginning is ā€œ{

ā€œcontent: [

{

ā€œelements":ā€

and it ends with ā€

ā€œtitle": ā€˜Modele contact-page’,

ā€œtype": ā€˜page’,

ā€œversion": ā€0.4ā€

}

ā€

but in the file generated by n8n, the beginning starts with : ā€œ[

{

ā€œdata": {

ā€œcontent": [

{

ā€œelements":ā€

And ends with : ā€

ā€œtitle": ā€˜Modele contact-page’,

ā€œtype": ā€˜page’,

ā€œversion": ā€0.4ā€

}

}

]

ā€

By removing this difference manually, the file becomes compatible with Elementor, and I can finally import it to appreciate the result. This is great!

But I’d like the JSON file generated by n8n to remove this difference on its own, so as to have exactly the same start and end structure as the original file. How can I make the generated file look just like the original without the ā€œdataā€ at the beginning?

Thanks again so much for your help!!!

here the workflow :

I need your help for the last dance to remove the difference beetween the 2 JSON files :sweat_smile:

Change the setting in the convert node to output each item as a separate file (see screenshot below). This should give you what you need, yes?!

Hello
I try what you say but nothing change. ā€œDataā€ is always here.

I’ve asked chatgpt to help me but the proposed solutions don’t help. Here’s what chatgpt replied:

The problem comes from the ā€œupdate translated fields in original ā€˜ node, which encapsulates the content in a ’dataā€ object.

As I said, the beginning must start with : {
ā€œcontentā€: [
{

and not with "data : "

Please show again the screenshot who you can see the difference

here is the problem

Yes, when you extract (JSON) data from a binary file, n8n groups your JSON as a collection of properties named after the given output field (ā€œdataā€ is default).

For your purpose, you want to flatten this structure (meaning: get rid of ā€œdataā€), as you don’t want this additional hierarchy in your final output. You can easily achieve this by adding .data in the first line of code (I am referring to the code node in the original workflow which I posted). This way, you access only the JSON that is within (or ā€œbeneathā€) the data property, if that makes sense.

// Get original structure and translated content
const original = $('Extract from File').all()[0].json.data;
const translated = $input.all()[0].json;

I hope, this helps. Cheers, Ingo

1 Like

Hello I find a solution with adding a new code node to achieve this :slight_smile:

Another thing… How I can make that the workflow generate 10 translatations in 10 differentes languages and then write 10 differents json files with dynamics name ?

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