Error while trying to load a JSON object with python 3

ChrisJ

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'

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

is Python code that defines a dictionary. If you had

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

you would have a string that contains a JSON object to be parsed. (Yes, there is a lot of overlap between Python syntax and JSON syntax.

assert customerData == json.loads(customerJSON)

would pass.)


Note, though, that not all valid Python resembles valid JSON.

Here are three different JSON strings that encode the same object:

json_strs = [
 "{'foo': 'bar'}",  # invalid JSON, uses single quotes
 '{"foo": "bar"}',  # valid JSON, uses double quotes
 '{foo: "bar"}'     # valid JSON, quotes around key can be omitted
]

You can observe that all(json.loads(x) == {'foo': 'bar'} for x in json_strs) is true, since all three strings encode the same Python dict.

Conversely, we can define three Python dicts, the first two of which are identical.

json_str = json_strs[0]  # Just to pick one
foo = ...  # Some value
dicts = [
  {'foo': 'bar'},     # valid Python dict
  {"foo": "bar"},     # valid Python dict
  {foo: "bar"}        # valid Python dict *if* foo is a hashable value
                      # and not necessarily 
]

It is true that dicts[0] == dicts[1] == json.loads(json_str). However, dicts[2] == json.loads(json_str) is only true if foo == "foo".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error while trying to load mapfragment

Validation error while trying to parse a json array to List[Object] in Scala

Error while trying to load module environment in makefile

There was an error while trying to load the gem 'yard'

Rails error while trying to load a Service in a controller

Getting error while trying to load laravel project

Python json.load JSONDecodeError: Extra data error when trying load multiple json dictionaries

Error when trying to load .json in webpage: Object reference not set to an instance of an object

ERROR NullInjectorError: StaticInjectorError(AppModule) while trying to load JSON data into table using *ngFor

Python giving vague error when trying to parse JSON object

Error trying to *ngFor a Json object

Trying to create object in main method , but getting error. Python 3

Getting error "pipeline element 3 is not an object error", while trying to find & update using aggregate

"TypeError: 'tuple' object is not callable" error in Python while trying to get a object in tuple by indexing

Matlab, error while trying to implement a Filter object

Python pymysql syntax error while inserting a JSON object

Expected object or value error while reading json file in python

Error while trying to fetch queryresult.KV object in JSON.Unmarshal

Error while Converting to Json Object

Error while Parsing JSON to Object

Error while JSON Object request

Ionic 3: Error trying to diff '[object Object]'

Error while trying to parse json into R

error while trying to parse JSON values

Error while trying to phrase JSON with Swift

Error while trying to iterate JSON array in JavaScript

Load nested json object in python

Error: TypeError: 'str' object cannot be interpreted as an integer while trying to pick a random winner (Python)

Getting error while trying to load data in mysql database