4

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'

6
  • 2
    That's not a JSON object; it's a fragment of Python code. customerData is already a dict, which is what you would be using json.loads to produce. Commented Mar 20, 2018 at 13:21
  • 1
    customerData already is a dictionary? Commented Mar 20, 2018 at 13:21
  • 1
    Do you mean you're trying to turn it into a JSON string? Then you need to go in the other direction: json.dumps() Commented Mar 20, 2018 at 13:21
  • 1
    @chepner Are you saying that I can use the data as is? Just treat it as I would a python dict? Commented Mar 20, 2018 at 13:28
  • 1
    Yes, because it is a Python dict, which is what the error message is telling you. JSON is just a language-independent string encoding of common data structures. The fact that the syntax is very similar to native Python syntax can be a source of confusion. (See the end of my answer for examples of things that are valid Python but not valid JSON and vice versa.) Commented Mar 20, 2018 at 13:34

2 Answers 2

15

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

Sign up to request clarification or add additional context in comments.

Comments

9
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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.