google-cloud-platformgoogle-apigoogle-natural-language

Json formatting for Google cloud NLP API (documents.analyzeEntities)


To send a document to the Cloud NLP API, we use the below JSON formating (as per the doc )

{
"document":{
            "type":"PLAIN_TEXT",
            "content":"Joanne Rowling, who writes under the pen names J. K. Rowling and Robert Galbraith, is a British novelist and screenwriter who wrote the Harry Potter fantasy series. i am suffering without resolution.only communication coming about fees without knowledge of card.wrist ever banking experience."
},
"encodingType":"UTF8"
}

This format works perfectly for the curl command i'm using.

curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" -s -X POST -H "Content-Type: application/json" --data-binary @request.json

However if want to add more than one document, how do i do it? I tried several ways, like making a List of dicts under 'document'. But it doesn't work. One of the attempt is shown below.

{
"document":{[{
            "type":"PLAIN_TEXT",
            "content":"Joanne Rowling, who writes under the pen names J. K. Rowling and Robert Galbraith, is a British novelist and screenwriter who wrote the Harry Potter fantasy series. i am suffering without resolution.only communication coming about fees without knowledge of card.wrist ever banking experience."
            },
            {
            "type":"PLAIN_TEXT",
            "content":"Joanne Rowling, who writes under the pen names J. K. Rowling and Robert Galbraith, is a British novelist and screenwriter who wrote the Harry Potter fantasy series. i am suffering without resolution.only communication coming about fees without knowledge of card.wrist ever banking experience."
            }
            ]},
"encodingType":"UTF8"
}

I get errors like

"message": "Invalid JSON payload received. Expected an object key or }.\n{\n\"document\":{[\n                {\"\n              ^",

Solution

  • At this time, the Cloud Natural Language API doesn't not accept multiple documents requests. You have to group in one text or send multiple requests.
    There is already a Feature Request for this to be implemented.
    Don't hesitate to add comments about your use case in order to let the product engineering team know about it.