I am producing a string using the jq
library, and when I print the string out to the console it results in a long string I am pasting at the bottom of my post. I store that string in out
. I then try to parse it into a dictionary via json.loads
json_data = json.loads(out)
This produces the following error:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/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
I also noticed that if I print the string to a file it looks funky:
"results":^[[1;39m[
^[[1;39m{
^[[0m^[[34;1m"name"^[[0m^[[1;39m: ^[[0m^[[0;32m"Jane Smith"^[[0m^[[1;39m,
^[[0m^[[34;1m"day"^[[0m^[[1;39m: ^[[0m^[[1;39m[
^[[1;39m{
^[[0m^[[34;1m"day"^[[0m^[[1;39m: ^[[0m^[[0;39m0^[[0m^[[1;39m,
^[[0m^[[34;1m"measurement_value"^[[0m^[[1;39m: ^[[0m^[[0;39m0.97^[[0m^[[1;39m
^[[1;39m}^[[0m^[[1;39m,
^[[1;39m{
^[[0m^[[34;1m"day"^[[0m^[[1;39m: ^[[0m^[[0;39m1^[[0m^[[1;39m,
^[[0m^[[34;1m"measurement_value"^[[0m^[[1;39m: ^[[0m^[[0;39m1.55^[[0m^[[1;39m
^[[1;39m}^[[0m^[[1;39m,
^[[1;39m{
So I am guessing something is going on with the encoding. What have I done wrong and what can I do to fix it so I can parse this string as JSON?
Here is the string.
{"results":[
{
"name": "Jane Smith",
"day": [
{
"day": 0,
"measurement_value": 0.97
},
{
"day": 1,
"measurement_value": 1.55
},
{
"day": 2,
"measurement_value": 0.67
}
]
},
{
"name": "Jane Smith",
"day": [
{
"day": 0,
"measurement_value": 1.25
},
{
"day": 1,
"measurement_value": 1.11
},
{
"day": 2,
"measurement_value": 0.067
}
]
},
{
"name": "Bob Smith",
"day": [
{
"day": 0,
"measurement_value": 0.97
},
{
"day": 1,
"measurement_value": 1.55
},
{
"day": 2,
"measurement_value": 0.67
}
]
},
{
"name": "Bob Smith",
"day": [
{
"day": 0,
"measurement_value": 1.25
},
{
"day": 1,
"measurement_value": 1.11
},
{
"day": 2,
"measurement_value": 0.067
}
]
}
]
}