Need a way to load embedded, escaped JSON strings in Python [duplicate] - python

This question already has answers here:
How can I parse (read) and use JSON?
(5 answers)
Closed last month.
I have to parse the following JSON string:
{"JobDescription":"{\"project\": \"1322\", \"vault\": \"qa-122\"}"}'
If I try to use json.loads, I get the following:
>>> import json
>>> print json.loads('{"JobDescription":"{\"project\": \"1322\", \"vault\": \"qa-122\"}"}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 22 (char 21)
I don't have any control over the string I receive as its generated by another system.

You are not producing embedded backslashes; Python is interpreting the \" as an escaped quote and the final string just contains the quote:
>>> '{"JobDescription":"{\"project\": \"1322\", \"vault\": \"qa-122\"}"}'
'{"JobDescription":"{"project": "1322", "vault": "qa-122"}"}'
Use a raw string or double the slashes:
>>> r'{"JobDescription":"{\"project\": \"1322\", \"vault\": \"qa-122\"}"}'
'{"JobDescription":"{\\"project\\": \\"1322\\", \\"vault\\": \\"qa-122\\"}"}'
>>> '{"JobDescription":"{\\"project\\": \\"1322\\", \\"vault\\": \\"qa-122\\"}"}'
'{"JobDescription":"{\\"project\\": \\"1322\\", \\"vault\\": \\"qa-122\\"}"}'
This then loads fine:
>>> import json
>>> json.loads('{"JobDescription":"{\\"project\\": \\"1322\\", \\"vault\\": \\"qa-122\\"}"}')
{'JobDescription': '{"project": "1322", "vault": "qa-122"}'}
and you can decode the nested JSON document from there:
>>> decoded = json.loads('{"JobDescription":"{\\"project\\": \\"1322\\", \\"vault\\": \\"qa-122\\"}"}')
>>> json.loads(decoded['JobDescription'])
{'project': '1322', 'vault': 'qa-122'}

Related

Error while converting a list to dict "json.decoder.JSONDecodeError: Expecting value"

import json
string = "'massage':'testing'"
json.loads(string)
But I get this error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/data/data/com.termux/files/usr/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/data/data/com.termux/files/usr/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/data/data/com.termux/files/usr/lib/python3.10/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 1 (char 0)
That is not valid JSON. This works:
import json
string = '{"message": "testing"}'
print(json.loads(string))
Not sure what this has to do with converting lists to dictionaries though.
according to the document
If the data being deserialized is not a valid JSON document, a
JSONDecodeError will be raised.
you have 2 mistake
you miss the brackets in your string
JSON specification - RFC7159 states that a string begins and ends with quotation mark.
That mean single quoute ' has no semantic meaning in JSON and is allowed only inside a string.
result:
import json
string = '{"massage":"testing"}'
json.loads(string)

How to read a JSON root array into a list?

I have a string containing a JSON array:
s = "['GY2_CAMP1', 'GY2_CAMP2', 'GY2_CAMP3', 'GY2_CAMP4', 'GY2_CAMP5']"
I tried to parse it as a JSON:
import json
l = json.loads(s)
I except to get a List, but an Exception has been raised:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/xx/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/home/xx/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/xx/anaconda3/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 1 column 2 (char 1)
I am not sure what happens here.
Use ast.literal_eval:
>>> from ast import literal_eval
>>> s = "['GY2_CAMP1', 'GY2_CAMP2', 'GY2_CAMP3', 'GY2_CAMP4', 'GY2_CAMP5']"
>>> literal_eval(s)
['GY2_CAMP1', 'GY2_CAMP2', 'GY2_CAMP3', 'GY2_CAMP4', 'GY2_CAMP5']
Json standard requires double quotes, it does not support single quotes.
That's why you get the error
Thus, the string should be
s = '["GY2_CAMP1", "GY2_CAMP2", "GY2_CAMP3", "GY2_CAMP4", "GY2_CAMP5"]'
Then you get the expected behaviour
import json
l = json.loads(s)
print(l)
>> ['GY2_CAMP1', 'GY2_CAMP2', 'GY2_CAMP3', 'GY2_CAMP4', 'GY2_CAMP5']
First replace the ' with " and then load.
import json
s = "['GY2_CAMP1', 'GY2_CAMP2', 'GY2_CAMP3', 'GY2_CAMP4', 'GY2_CAMP5']"
s.replace("'", "\"")
l = json.loads(s1)
print(l)
will get
['GY2_CAMP1', 'GY2_CAMP2', 'GY2_CAMP3', 'GY2_CAMP4', 'GY2_CAMP5']

Can't create JSON doc from dict with string containing line feed chars

I'm creating a JSON structure which I ultimately need to save to a file but am having problems with embedded line feed characters.
I first create a dictionary:
changes = {
"20161101": "Added logging",
"20161027": "Fixed scrolling bug",
"20161024": "Added summary functionality"
}
and then convert it to a single line-feed separated string:
changes_str = '\n'.join([ "{0} - {1}".format(x, y) for x, y in changes.items() ])
print changes_str
'20161101 - Added logging\n20161027 - Fixed scrolling bug\n20161024 - Added summary functionality'
So far, so good. Now I add it into string (which in reality would come from a text template):
changes_str_json_str = '{ "version": 1.1, "changes": "' + changes_str + '" }'
print changes_str_json_str
'{ "version": 1.1, "changes": 20161101 - Added logging\n20161027 - Fixed scrolling bug\n20161024 - Added summary functionality }'
but when I come to create / encode a JSON object from this using loads, I hit problems:
json_obj = json.loads(changes_str_json_str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/opt/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 1 column 55 (char 54)
Changing the line feed to another character does fix the problem so clearly that's where the problem lies, however, I do need the character to be a line feed as ultimately the data in the file needs to be formatted like this (the file is passed on to another system over which I have no control. Also, as far as I know, line feed is a supported character in JSON strings.
What exactly is the problem here and how can I work around it?
In JSON you need to properly escape the control characters including \n. Here's example on what's currently happening:
>>> import json
>>> json.loads('"foo\nbar"')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\python35\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\python35\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\python35\lib\json\decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 5 (char 4)
If you properly escape the newline character with backslash it will work as expected:
>>> json.loads('"foo\\nbar"')
'foo\nbar'
So you could fix your code by doing following:
changes_str = '\\n'.join([ "{0} - {1}".format(x, y) for x, y in changes.items() ])
The better alternative would be to first construct the object you want to output and then use dumps so you wouldn't have to worry about escaping at all:
obj = {
'version': 1.1,
'changes': changes_str
}
changes_str_json_str = json.dumps(obj)
To convert it to a single line-feed separated string:
import json
changes_str = json.dumps(changes)
To load a string JSON in dict python:
dict_changes = json.loads(changes_str)

Error Loading JSON from configuration file using ConfigParser

I have this configuration file
[section1]
namespace = {'pro1':'http://pro1/go','pro2':'http://pro2/go','pro3':'http://pro3/go'}
Reading the file with ConfigParser I got:
>>> from ConfigParser import ConfigParser
>>> Config = ConfigParser()
>>> Config.read("myfile.ini")
>>> name = Config.get("section1", "namespace")
>>> name
"{'pro1':'http://pro1/go','pro2':'http://pro2/go','pro3':'http://pro3/go'}"
To convert the string to dict/json:
>>> import json
>>> namespace = json.loads(name)
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 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
>>>
Any ideas?
That's not valid JSON; unlike Python dicts, JSON is very strict - you always need to use double quotes.
namespace = {"pro1":"http://pro1/go","pro2":"http://pro2/go","pro3":"http://pro3/go"}

Issue in reading JSON file in python

>>> import json
>>> d2 = json.loads(open("t.json").read())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/usr/lib64/python2.6/json/decoder.py", line 185, in JSONObject
raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 1 column 11 (char 11)
[ RHEL - ~/testing ]$ cat t.json
{"us": u"OFF", "val": u"5"}
Here is what I have in my JSON file and when I try to read it using open and json.load and json.loads it fails.
After using json.load
>>> import json
>>> d2 = json.load(open("t.json"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 267, in load
parse_constant=parse_constant, **kw)
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/usr/lib64/python2.6/json/decoder.py", line 185, in JSONObject
raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 1 column 11 (char 11)
>>>
You are using the wrong function. Use json.load() (no s!) to load data from an open file object:
d2 = json.load(open("t.json"))
The json.loads() function expects you to pass in a string, not a file object. You'd have to read your file in that case, returning the read data:
d2 = json.loads(open("t.json").read())
Next, you have invalid JSON in that file:
{"us": u"OFF", "val": u"5"}
# ^ ^
JSON is not Python; those u prefixes are not supported nor needed. You'll need to remove those from the file before it'll load.
If you have an API producing that format, it is not giving you JSON. It could be that it is producing a (strange form of) Python syntax instead; Python itself would produce {'us': u'OFF', 'val': u'5'} (single quotes). You can have Python interpret that as Python literals with ast.literal_eval():
import ast
with open('t.json') as fileobj:
d2 = ast.literal_eval(fileobj.read())
but it could be that the format is broken in other ways we cannot determine from a single isolated sample. It could be using true and false for boolean values, like in JSON, for example.
Better to have the API fixed rather that try and work around this broken-ness.
You are using the json.loads method. More documentation here. This method is used for string arguments only. Luckily, there is a similarly named json.load method documented here. This one can be used directly on a file object.
d2 = json.load(open("t.json"))
Your issue is that the JSON is not valid.
It looks like it is a python dictionnary. u'string' is a python 2 unicode string.
If you remove the u from your strings, it works fine.
>>> import json
>>> json.load(open('i.json'))
{u'val': u'5', u'us': u'OFF'}
Here is the json file:
$ cat i.json
{"us": "OFF", "val": "5"}

Categories