Help creating JSON using VaRest

Hi there, in my project blueprint I’m trying to use Google Natural Language APIs but can’t find a way to serialize a correct json.
I need a json like this:
{
“document”: {
“type”: “PLAIN_TEXT”,
“language”: “it”,
“content”: “domanda da analizzare”
},
“encodingType”: “UTF8”
}

Where “content” is a string type variable in my blueprint.

How can I make this JSON using VaRest to send to to the service?

Thanks in advance
Danilo

It posts on z key :stuck_out_tongue:

Thanks for your help but “content” should be a variable.
I’m trying something here

But in print string i got the json like this:
{“document”:“{"type":"PLAIN_TEXT","language":"it","content":"ciao! oggi sono stato molto bene e sono molto contento"}”,“encodingType”:“UTF8”}

And it can’t read it properly.

Decode to json object creates the valid json object without the need for specific nesting

{
"document": {
"type": "PLAIN_TEXT",
"language": "it",
"content": "domanda da analizzare"
},
"encodingType": "UTF8"
}

The problem is probably with your double quotation marks. They are invalid.

You seem to have “ and ” instead of just "

Once the quotation marks are corrected it validates

Thanks for your answer but I don’t know how to change this because VaRest is doing all the job.
This is the full setup for this JSON i need:

In the first “Print String” node the string from “Encode JSON to single string” is:

{"type":"PLAIN_TEXT","language":"it","content":"ciao, oggi sono stato molto bene e sono molto contento"}

In the second “Print String” node the string from “Encode JSON to single string” is:

{"document":"{\"type\":\"PLAIN_TEXT\",\"language\":\"it\",\"content\":\"ciao, oggi sono stato molto bene e sono molto contento\"}","encodingType":"UTF8"}

The JSON should be like this:

{
  "document": {
    "type": "PLAIN_TEXT",
    "language": "it",
    "content": "ciao, oggi sono stato molto bene e sono molto contento"
  },
  "encodingType": "UTF8"
}

I tried other nodes and functions but nothing is changing something so I think the problem could be in “Encode JSON to single string” node, but I am not a pro so I really don’t know.

May there is another way to setup a JSON like this with VaRest but I don’t know about it.

I showed you how to get it working by using Decode Json Object. It takes the direct string and convets it to a json object that varest uses directly.

Just make sure the information in the literal string has the correct quotation marks.

It seems that is not working with “decote json object”.
I tryed to clean the string replacing "" with " " but even if in “print string” I see it correctly when I try to post it to google service there are "" in the string again.

I don’t know.
Maybe I placed “decode” in the wrong position idk.

Even if I clean the string like this:

Providing this string:

{"document":{ "type ": "PLAIN_TEXT ", "language ": "it ", "content ": "ciao, oggi sono stato molto bene e sono molto contento "},"encodingType":"UTF8"}

Google Natural Language response is:

LogVaRest: Request (json): POST https://language.googleapis.com/v1beta2/documents:analyzeSentiment?key=***** (check bExtendedLog for additional data)
LogVaRest: Response (400): 
JSON(
{
  "error": {
    "code": 400,
    "message": "Invalid value at 'document' (type.googleapis.com/google.cloud.language.v1beta2.Document), \"{\"document\":{ \"type \": \"PLAIN_TEXT \", \"language \": \"it \", \"content \": \"ciao, oggi sono stato molto bene e sono molto contento \"},\"encodingType\":\"UTF8\"}\"",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "document",
            "description": "Invalid value at 'document' (type.googleapis.com/google.cloud.language.v1beta2.Document), \"{\"document\":{ \"type \": \"PLAIN_TEXT \", \"language \": \"it \", \"content \": \"ciao, oggi sono stato molto bene e sono molto contento \"},\"encodingType\":\"UTF8\"}\""
          }
        ]
      }
    ]
  }
}
)JSON

Solved with the “Set object field” node.

This is new working setup for me.

Thanks for help.
Danilo :slight_smile:

1 Like