Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while trying to load a JSON object with python 3

Tags:

python

json

I have the following json object I am trying to parse with python 3:

customerData = {   
 "Joe": {"visits": 1},  
 "Carol":  {"visits": 2},  
 "Howard": {"visits": 3},  
 "Carrie": {"visits": 4}  
}

I am using the following python code to parse the object:

import json 

def greetCustomer(customerData):
    response = json.loads(customerData)

I'm getting the following error:

TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

like image 327
ChrisJ Avatar asked Nov 28 '22 13:11

ChrisJ


1 Answers

You seem to be mistaking load and dump.

json.loads converts a string to a python object, json.load converts a json file into a python object whereas json.dumps converts a python object to a string and json.dump writes a json string to a file from a python object

Tip: notice that loads and dumps have an s at the end, as in string

like image 111
ted Avatar answered Dec 10 '22 15:12

ted