I am trying to convert a dictionary object into json, but unable to do so. I tried json.load(), json.loads() but it gives me following error:
File "lean.py", line 252, in createJSON
return json.loads(jsonElementDict)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
When I print dictionary object, it's as follows:
{
'city': 'Pittsburgh',
'state': 'Pennsylvania',
'gross_floor_area': '',
'energyData': [
{
'cdd': '0.0',
'reading': '80.8',
'monthYear': 'Nov-2014'
},
{
'cdd': '0.0',
'reading': '300000.0',
'monthYear': 'Nov-2014'
}
]
}`
Code:
outputJSON = json.loads(jsonElementDict)
print outputJSON.json['city']
Related
While trying to convert the following string into JSON object, I got an error. How can I fix it?
text = '{ "MonitorGroupGuid": "e8b20230-70b6-4348-36f3e3f", "Description": "Root CA", "IsAll":False}'
JsonObject = json.loads(text)
Output:
x = json.loads(x)
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 98 (char 97)
You can simply use "" for False are use 0/1, but if you want it as a bool object use false without quotes
"False" datatype is string
just false datatype bool
text = '{ "MonitorGroupGuid": "e8b20230-70b6-4348-36f3e3f", "Description": "Root CA", "IsAll":"False"}'
JsonObject = json.loads(text)
print(JsonObject)#{'MonitorGroupGuid': 'e8b20230-70b6-4348-36f3e3f', 'Description': 'Root CA', 'IsAll': 'False'}
Replace False with false (without quotes)
text = '{ "MonitorGroupGuid": "e8b20230-70b6-4348-36f3e3f", "Description": "Root CA", "IsAll":false}'
JsonObject = json.loads(text)
print(JsonObject) #{'MonitorGroupGuid': 'e8b20230-70b6-4348-36f3e3f', 'Description': 'Root CA', 'IsAll': False}
I have a simple python program that should load a .json file into a variable and print the variable output to the screen. But it fails to run and errors with the following message:
"File "/Documents/VSCode/python-3-programming-specialization/Data Collection and Processing with Python (Course 3)/Lesson 1/assessment6.py", line 5, in <module>
res = json.load(json_file,)
File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 15 column 25 (char 506)"
In the JSON file there is an error too that I think is the problem but I don't know what values to set to clear the error in the JSON file. The error occurs anywhere you see a None, True or False value.
JSON error message: "Value expected json(516)
"statuses": [
{
"contributors": None,
"truncated": False,
"text": "RT #mikeweber25: I'm decommiting from the university of Michigan thank you Michigan for the love and support I'll remake my decision at the\u2026",
"in_reply_to_status_id": None,
"id": 536624519285583872,
"favorite_count": 0,
"source": "Twitter for iPhone",
"retweeted": False,
"coordinates": None,
import json
with open('./tweets.json') as json_file:
res = json.load(json_file)
print(res)
The expected output should be the file read in by the python program.
I made a some code
jsonFile = json.loads(data.data, 'utf-8')
print 'jsonFile'
the result is:
{u'INFO': {u'CALL': u'000000000000', u'MODULE': u'POWERVOICE'}, u'PersonType': u'Caller', u'KEYWORD': {u'start': 720, u'end': 1920, u'value': [u'\ubc31\ud654\uc810']}, u'END_FLAG': 0, u'TEXT': {u'start': 720, u'end': 1920, u'value': u'\ub730\uc885\ud569\ubc31\ud654\uc810'}, u'FID_INFO': {u'SID': u'123456789', u'CallConfidence': u'0.123', u'FakeCall': u'1'}, u'POI': {u'start': 720, u'end': 1920, u'value': [u'']}, u'SITUATION': {u'Fire': 0, u'FirstAid': 0, u'Rescue': 0}}
but error happens
bad callback: <function resultCallback at 0x7f1ac4ce6c08>
Traceback (most recent call last):
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720, in _invoke_callback
cb(msg)
File "/root/catkin_ws/src/listen_project/src/node_evaluate.py", line 58, in resultCallback
jsonFile = json.loads(data.data, 'utf-8')
File "/usr/lib/python2.7/json/__init__.py", line 351, in loads
return cls(encoding=encoding, **kw).decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
-------------------------------------
Is my json file not validate?
thank you.
You have a python literal, not a json object. (which may be loaded with ast.literal_eval)
json uses double quoted strings for everything.
It's not entirely clear from the code posted, data.data may already be a python object?
The error implies you are attempting to loads the empty string. Can you show what data.data is?
I am trying to print below json output using below script,
Json Sample:
[{"id":"56cd7e4d2d0edcace915e674","protocol":"https","hostName":"127.0.0.1","port":443,"serverName":"Site1","status":"connected","connected":true}]
Code i have used:
import Requests
response = requests.get("https://Site1/rest/settings/all-server-status",params={'serverId': '56cd7e4d2d0edcace915e674'}, verify=False)
json_data = json.loads(response.text)
When i am trying to print json_data i got below error,
Traceback (most recent call last):
File "<pyshell#116>", line 1, in <module>
json_data = json.loads(response.text)
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
Please help me on this, Thanks in advance!!
I have
import json
a = {'code': 'exam', 'list': [{'note': '2', 'right': '2', 'question': 'Tr\xe0n V?n H\xf9ng', 'answers': ['etreetetetetret', 'reteretet', 'tedtetetet', 'etetetet']}], 'id': 1, 'level': 1}
json.dumps(a)
===> error: UnicodeDecodeError: 'utf8' codec can't decode byte 0xe0 in position 2: invalid
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe0 in position 2: invalid continuation byte
Any byte strings (in Python 2 any string not a unicode string is a byte string) is decoded to Unicode first when creating the JSON output. The json.dumps() method by default uses UTF-8 for that; your input data is not using UTF-8 however.
Tell json.dumps() what encoding to use instead, or decode your strings to unicode yourself. Here, you appear to be using Latin-1 strings, so use that:
json.dumps(a, encoding='latin1')
Demo:
>>> import json
>>> a = {'code': 'exam', 'list': [{'note': '2', 'right': '2', 'question': 'Tr\xe0n V?n H\xf9ng', 'answers': ['etreetetetetret', 'reteretet', 'tedtetetet', 'etetetet']}], 'id': 1, 'level': 1}
>>> json.dumps(a, encoding='latin1')
'{"code": "exam", "list": [{"note": "2", "right": "2", "question": "Tr\\u00e0n V?n H\\u00f9ng", "answers": ["etreetetetetret", "reteretet", "tedtetetet", "etetetet"]}], "id": 1, "level": 1}'