简体   繁体   中英

Using json.dumps with a nested dictionary

I have the following code which errors out

def generate_search_query(matterID,leaver_user,vaultAccessToken):
    for user in leaver_user:
        url = "https://vault.googleapis.com/v1/matters/"+matterID+"/savedQueries"

        headers = {
            "Accept" : "application/json",
            "Content-Type" : "application/json",
            "Authorization": "Bearer " + vaultAccessToken
        }

        query={
            "query": {
            "corpus": "MAIL",
            "dataScope": "ALL_DATA",
            "searchMethod": "ACCOUNT",
            "accountInfo": { "emails": [user]},
            "mailOptions": {"excludeDrafts : false"},
            "timeZone": "Atlantic/Canary",
            "method": "ACCOUNT"
            }
        }

        body=json.dumps({
            "displayName": "test",
            "query": {
                "corpus": "MAIL",
                "dataScope": "ALL_DATA",
                "searchMethod": "ACCOUNT",
                "accountInfo": { "emails": [user]},
                "mailOptions": {"excludeDrafts : false"},
                "timeZone": "Atlantic/Canary",
                "method": "ACCOUNT"
            }}
        )


        response = requests.request(
            "POST",
            url,
            headers=headers,
            data=body
        )
        print(response.content)

Which outputs TypeError: Object of type set is not JSON serializable when executed.

I've tried to use the below code which works however unfortunately the Google API is very picky unless I use json.dumps() .

query={
    "query": {
        "corpus": "MAIL",
        "dataScope": "ALL_DATA",
        "searchMethod": "ACCOUNT",
        "accountInfo": { "emails": [user]},
        "mailOptions": {"excludeDrafts : false"},
        "timeZone": "Atlantic/Canary",
        "method": "ACCOUNT"
        }
}


body = {"displayName": user + "email search query",}

data = {"displayName": body,"query": query}

response = requests.request(
    "POST",
    url,
    headers=headers,
    data=data
)
print(response.content)

Then this is returned from the Google Vault API

b'{\n "error": {\n "code": 400,\n "message": "Invalid JSON payload received. Unexpected token.\\ndisplayName=displayN\\n^",\n "status": "INVALID_ARGUMENT"\n }\n}\n'

Then if I use

body =json.dumps( {"displayName": user + "email search query",})
data =json.dumps( {"displayName": body,"query": query})

I get a Python error

TypeError: Object of type set is not JSON serializable

What's the best way to solve this?

It seems that your nested object contain on object of type set.

You can quickly replicate the same error with following

json.dumps({'test': set([1, 2])})

You can solve this by transforming the set into a list.

json.dumps({'test': list(set([1, 2]))})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM