Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that jsonStr is a string that encodes some object in JSON, not the actual object.</p> <p>You obviously <em>knew</em> it was a string, because you called it <code>jsonStr</code>. And it's proven by the fact that this line works:</p> <pre><code>jsonStr = data.decode("utf-8") </code></pre> <p>So, <code>jsonStr</code> is a string. Calling <code>json.dumps</code> on a string is perfectly legal. It doesn't matter whether that string was the JSON encoding of some object, or your last name; you can encode that string in JSON. And then you can decode that string, getting back the original string.</p> <p>So, this:</p> <pre><code>j = json.loads(json.dumps(jsonStr)) </code></pre> <p>… is going to give you back the exact same string as <code>jsonStr</code> in <code>j</code>. Which you still haven't <em>decoded</em> to the original object.</p> <p>To do that, just don't do the extra encode:</p> <pre><code>j = json.loads(jsonStr) </code></pre> <p>If that isn't clear, try playing with it an interactive terminal:</p> <pre><code>&gt;&gt;&gt; obj = ['abc', {'a', 'b': 2}] &gt;&gt;&gt; type(obj) list &gt;&gt;&gt; obj[1]['b'] 2 &gt;&gt;&gt; j = json.dumps(obj) &gt;&gt;&gt; type(j) str &gt;&gt;&gt; j[1]['b'] TypeError: string indices must be integers, not str &gt;&gt;&gt; jj = json.dumps(obj) &gt;&gt;&gt; type(jj) str &gt;&gt;&gt; j '["abc", {"a": 1, "b": 2}]' &gt;&gt;&gt; jj '"[\\"abc\\", {\\"a\\": 1, \\"b\\": 2}]"' &gt;&gt;&gt; json.loads(j) ['abc', {'a': 1, 'b': 2}] &gt;&gt;&gt; json.loads(j) == obj True &gt;&gt;&gt; json.loads(jj) '["abc", {"a": 1, "b": 2}]' &gt;&gt;&gt; json.loads(jj) == j True &gt;&gt;&gt; json.loads(jj) == obj False </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload