0

Hi I am working with JSON in my file in Python:

import json
userData = '''[
{
    "userID" : "20",
    "devices" : {
        "360020000147343433313337" : "V33_03",
        "3f0026001747343438323536" : "Door_03",
        "170035001247343438323536" : "IR_06",
        "28004c000651353530373132" : "BED_17"
    }
},

]'''

info = json.loads(userData)

I get this error when I load it: json.decoder.JSONDecodeError: Expecting value:

or sometimes when I add something: json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes:

4 Answers 4

3

Try using the ast module

Ex:

import ast
userData = '''[
{
    "userID" : "20",
    "devices" : {
        "360020000147343433313337" : "V33_03",
        "3f0026001747343438323536" : "Door_03",
        "170035001247343438323536" : "IR_06",
        "28004c000651353530373132" : "BED_17"
    }
},
]'''

info = ast.literal_eval(userData)
print(info)
Sign up to request clarification or add additional context in comments.

3 Comments

Is this a JSON object now?
What do you mean by JSON object? using ast converts your string data-structure to valid python data-type..like a dict same as json.loads
I can get objects by info['userID'] ?, Yeah just tested it, I can go for this. However, still wondering that didn't work...
1

Looks the format is incorrect.

userData = '''[
{
    "userID" : "20",
    "devices" : {
        "360020000147343433313337" : "V33_03",
        "3f0026001747343438323536" : "Door_03",
        "170035001247343438323536" : "IR_06",
        "28004c000651353530373132" : "BED_17"
    }
},  <--- remove this ","

]'''

See my test:

>>> import json
>>> json.loads('[{"a":"b"}]')
[{u'a': u'b'}]
>>> json.loads('[{"a":"b"},]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
>>>

Comments

1

For future reference, below how to get the JSON content or to get some spam:

import requests


url = 'http://httpbin.org/status/200'
r = requests.get(url)

if 'json' in r.headers.get('Content-Type'):
    js = r.json()
else:
    print('Response content is not in JSON format.')
    js = 'spam'

1 Comment

Does this help with the original question? (identifying the problem within the JSON string)
0

With your example as-is and no further understanding: info = json.loads(json.dumps(userData)) will work.

There are a lot of posts on SO about python multi-line strings and JSON. Ideally you would not be loading a string from a string variable that way is the general comment you will see.

With some additional explanation, such as where does the data originate and in what format, I can provide additional assistance.

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.