Python Forum
Invalid JSON payload received. Unknown name “”: Root element must be a message."
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid JSON payload received. Unknown name “”: Root element must be a message."
#1
Hi guys,

I am trying to update a Google contact using People API. Unfortunately I am getting an error:

Invalid JSON payload received. Unknown name "": Root element must be a message.

when executing the request.

Anyone has any clue how can I resolve it? The JSON itself seems to be OK because I used https://developers.google.com/people/api...ateContact to test it, got 200 response and the contact was modified.

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request


# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/contacts']

def modifyContact(service, resourceName, JSON):
    print (JSON)
    service.people().updateContact(resourceName=resourceName, updatePersonFields='phoneNumbers', body=JSON).execute()

def buildJSON(phoneNumbers, etag):
    JSON = ""
    for x in range(0, len(phoneNumbers)):
        if phoneNumbers[x].get('type') == 'mobile':
            if phoneNumbers[x].get('value').find('+48') != -1:
                oldNUmber = phoneNumbers[x].get('value')
                newNumber = phoneNumbers[x].get('value')[3:]
                JSON += """
                        {
                            "type": "mobile",
                            "value": "%s"
                        },
                        {
                            "type": "mobile",
                            "value": "%s"
                        },
                        """ % (oldNUmber, newNumber)
            else:
                JSON += """
                        {
                            "type": "mobile",
                            "value": "%s"
                        },
                        """ % (phoneNumbers[x].get('value'))
        else:
            JSON += """
                        {
                          "type": "%s",
                            "value": "%s"
                        },
                    """ % (phoneNumbers[x].get('type'), phoneNumbers[x].get('value'))
    # remove last whitespaces + character which is exceeding comma
    JSON = JSON.rstrip()[:-1]
    JSON = """
              "phoneNumbers": [
                %s
              ],
              "etag": "%s"
            """ % (JSON, etag)
    #print (JSON)
    return JSON

def main():

    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('people', 'v1', credentials=creds)





    # Call the People API
    print('List 10 connection names')
    results = service.people().connections().list(
        resourceName='people/me',
        pageSize=1000,
        personFields='names,phoneNumbers').execute()
    connections = results.get('connections', [])

    for person in connections:
        names = person.get('names', [])
        phoneNumbers = person.get('phoneNumbers', [])
        if names:
            name = names[0].get('givenName')
            if name == "testAPI":
                print(name)
                print (person.get('etag'))
                print (person.get('resourceName'))
                if phoneNumbers:
                    JSON = buildJSON(phoneNumbers, person.get('etag'))
                    modifyContact(service, person.get('resourceName'), JSON)
                    #print (phoneNumbers[x].get('value'))
                print (person)
if __name__ == '__main__':
    main()
Reply
#2
Why didn't you use standard Python module json to build json structures, e.g.
import json
json.dumps([{'type': 'mobile', 'value' : 4}, {'type': 'mobile', 'value' : 3}])
Output:
'[{"type": "mobile", "value": 4}, {"type": "mobile", "value": 3}]'
Using json.dumps is more reliable approach; I am not sure about json-structure that you built '{some content}, {some content}', may be valid version would be '[{some content}, {some content}]'.
Reply
#3
Thanks scidam for your reply. Would it make any difference for a service if I make a string containing necessary data than using json.dumps? As per my understaning JSON is a simple text format having specific structure and thats it.
Reply
#4
(Aug-18-2019, 01:52 PM)hellraiser Wrote: Would it make any difference for a service if I make a string containing necessary data than using json.dumps?
it will make your code better (or at least that particular json-building part of it)
as to your question - show us the JSON that you get. Looking at lines 49-54 I think it's not a valid json. You can use https://jsonlint.com/ to check for yourself
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Aug-18-2019, 01:59 PM)buran Wrote: it will make your code better (or at least that particular json-building part of it)
as to your question - show us the JSON that you get. Looking at lines 49-54 I think it's not a valid json. You can use https://jsonlint.com/ to check for yourself

hi buran,
Thanks for the help and clarifications. I am still learning both python and JSON and I am not sure how could I pass a value (variable) to JSON that I am building using the way showed by scidam:
json.dumps([{'type': 'mobile', 'value' : 4}, {'type': 'mobile', 'value' : 3}])
Anyway, I solved the issue! :)

First of all when I was troubleshooting I've removed the initial/ending brackets from JSON string.

Should be:
    JSON = """{
                  "phoneNumbers": [
                    %s
                  ],
                  "etag": "%s"
              }
            """ % (JSON, etag)
And second, I had to deserialize JSON string with loads()

    return json.loads(JSON)
And now it works like a charm :)

Thanks guys for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Json filter is not capturing desired key/element mrapple2020 1 1,073 Nov-24-2022, 09:22 AM
Last Post: ibreeden
Question How to understand the received bytes of ser.read? pf2022 3 1,921 Mar-24-2022, 11:37 AM
Last Post: pf2022
  Attribute Error received not understood (Please Help) crocolicious 5 2,613 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,741 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  How to continue in loop until correct input received sunnyarora 10 9,775 May-04-2019, 02:37 PM
Last Post: Yoriz
  unable to pass a input after changing the user from root to non root using python avinash 3 3,128 Apr-08-2019, 10:05 AM
Last Post: avinash
  Unable to locate element no such element gahhon 6 4,370 Feb-18-2019, 02:09 PM
Last Post: gahhon
  assign the variable to every value received serial from Arduino barry76 4 3,284 Feb-01-2019, 10:19 AM
Last Post: barry76
  Error message in Jupyter Notebook with json diet 4 5,492 Jun-17-2018, 08:32 PM
Last Post: snippsat
  need help in passing url as variable in payload vkvasu25 6 5,918 Apr-20-2018, 12:53 PM
Last Post: vkvasu25

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020