1

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
      }
    ]
  }
]
}
helloB
  • 3,472
  • 10
  • 40
  • 87
  • Skip the step of reading the response into a string, and just pass the response to `json.load()`. I suspect you are not creating the string properly, anyways, because `json.loads()` works fine for me with that data in a string. – Bob Dylan Mar 10 '16 at 21:16
  • 1
    Where is that string coming from? You seem to have a load of ANSI codes, which are used for things like changing colours in the terminal. How did they get there? – Daniel Roseman Mar 10 '16 at 21:26
  • As mentioned, you appear to have downloaded a pretty-printed version of the string. After you paste it to stackoverflow and we copy it, the pretty printing is stripped out and your example works. So, you need something that strips them. This post [Filtering out ANSI escape sequences](http://stackoverflow.com/questions/13506033/filtering-out-ansi-escape-sequences) suggests the the regex `r"/(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]/"` will remove all of the escapes. The post has other suggestions also. – tdelaney Mar 10 '16 at 21:44
  • @DanielRoseman I used sh and jq to parse another JSON file. – helloB Mar 11 '16 at 09:26

0 Answers0