Need help parsing a list into JSON for API call

Hey team!

I have been stuck at this for a while now and cannot seem to find the answer specific to my case in previous posts.

In my app I have a data type for a sentence. The first field is text. Here is an image of a few sentence rows where the text is displayed in a repeating group:

Screenshot 2022-07-21 at 06.20.02

Context
I now need to translate the sentences in bulk. Logically, I want to make as little API calls as possible. For a translation engine I chose DeepL. For translating multiple sentences at a time, their docs show the following:

This tells me I need to some way to pass a list of sentence texts’ to the API. I started doing some research and read this post: Array of parameters for an API - #10 by prajnyabaliga3186. Keith’s answer really helped me understand what I am dealing with. I did not manage to make it work though :frowning:

What I do not understand
I am looking for help with how to format (or parse) the text. I need to do this in the JSON body field of the plugin editor.

DeepL description of the text parameter: “Text to be translated. Only UTF8-encoded plain text is supported. The parameter may be specified multiple times and translations are returned in the same order as they are requested. Each of the parameter values may contain multiple sentences. Up to 50 texts can be sent for translation in one request.”

I have tried setups like this:

But when initialising the call it returns an error saying code 400, “Parameter ‘text’ not specified”

Single translations work fine. But this formatting trick I have yet to learn! Any help would be greatly appreciated! Cheers guys :slight_smile:

Try making your "Body (JSON Object) match the same structure as in the example. You need to start with an object: {} and then that object needs a key called “translations” which is an array of objects: [{}]. and each object in that array has a key of “text”. That is the structure it is looking for and you are only giving an object.

{
     "translations": [
       {
        "text": "<text>",
        "target_lang": "<target_lang>"
      }
      ]
}

Also the value of text cannot be an array. If you will pass up to fifty at any given time then you have to add this 50 times and put allow blank for the times you dont use all 50:
{ "text": "<text>", "target_lang": "<target_lang>" }

1 Like

Hi William, thanks for your time!

I’ve adjusted the code to send 3 sentences at a time (I think). I’m trying to only send one and allowed for blanks. How do you think I should format the text value to initialize the api call?

When I try with a single value:

It returns the following error:
Screenshot 2022-07-21 at 20.01.31

Thanks again for any help :grin:

You have missing comma after the first two object for text
However you should not encoded your json this way. You will lack of flexibility around the text tring
You shoud use
{"translaction":"[<texttotranslate>]}
In the field you will set {“text”:“the text to translate here”,“target_lang”:“EN”}
Initialize this way
In WF or Get data call, you will use :format as text to format it like {"text":"this thing text(dynamic)","target_lang":"This thing language(dynamic)"} with delimiter set to comma (,)

3 Likes

That’s very good to know. I didn’t know there was a way to make these dynamic in length!

2 Likes

Cheers both! Will try this as soon as I can. Report back later :+1:

1 Like

Hi @Jici
I followed what you wrote but i get invalid JSON object error
Can you please tell me what i did wrong?

Screenshot 2022-07-22 at 16.49.16


Screenshot 2022-07-22 at 16.48.43

You doesn’t have doublequotes around some dynamic value. I guess you need quotes around each of them but you need to check API Doc for the fields.

Also, the list of skills probable need to be format like: [“ThisVerso_job’s skills:join with (”,")"]
If this can be empty you will need to format as text too

2 Likes

Hey Jici, thanks again!

I am not sure about if I need to define shared parameters for the call. Logically I have to define the two required parameters ‘text’ and ‘target_lang’, right?

Anyway, I built the object as you described (with one extra " to close the dynamic values) and tried to initialize the workflow as following:

Error message is still that the text parameter isn’t defined… :no_mouth:

Remove parameters. You have theses parameters in your body. Do the API expect a json or url form encoded?

Update: I did it. Thanks everyone for tips or asking the right questions! Apparently I do not even need to parse JSON but simply use parameters and text formatting. Just sharing the results/solution here for future references.

Here is a screenshot of the plugin editor (working):

The text value is formatted as:

Hoi Mijn naam is James",“Ha beste, hoe is het?”,"Proost jongen!

Notice the delimiter “,” is not needed up front and at the end.

I can now initialize the call and save the plugin. Great step towards finishing my MVP. Cheers again for all the help!

Screenshot of result:
Screenshot 2022-07-24 at 09.02.27

:partying_face:

1 Like