Parse decodeURIComponent JSON string with Python
<p>I have a "deep" JSON string that I need to pass as GET vars in a URL. It looks like the following:</p>

<pre><code>{
  "meta": {
    "prune": true,
    "returnFields": ["gf", "gh", "gh", "rt"],
    "orient": "split"
  },
  "indicators": [{
    "type": "beta",
    "computeOn": "gf",
    "parameters": {
      "timeperiod": 5,
      "nbdevup": 2,
      "nbdevdn": 2,
      "matype": 0
    }
  }, {
    "type": "alpha",
    "computeOn": "gf",
    "parameters": {
      "timeperiod": 30
    }
  }]
};
</code></pre>

<p>When encoding using <code>jQuery.param</code>, the result is as follows:</p>

<pre><code>var recursiveEncoded = jQuery.param(body);
console.log(recursiveEncoded);
meta%5Bprune%5D=true&amp;meta%5BreturnFields%5D%5B%5D=gf&amp;meta%5BreturnFields%5D%5B%5D=gh&amp;meta%5BreturnFields%5D%5B%5D=gh&amp;meta%5BreturnFields%5D%5B%5D=rt&amp;meta%5Borient%5D=split&amp;indicators%5B0%5D%5Btype%5D=beta&amp;indicators%5B0%5D%5BcomputeOn%5D=gf&amp;indicators%5B0%5D%5Bparameters%5D%5Btimeperiod%5D=5&amp;indicators%5B0%5D%5Bparameters%5D%5Bnbdevup%5D=2&amp;indicators%5B0%5D%5Bparameters%5D%5Bnbdevdn%5D=2&amp;indicators%5B0%5D%5Bparameters%5D%5Bmatype%5D=0&amp;indicators%5B1%5D%5Btype%5D=alpha&amp;indicators%5B1%5D%5BcomputeOn%5D=gf&amp;indicators%5B1%5D%5Bparameters%5D%5Btimeperiod%5D=30
</code></pre>

<p>Which is decoded to the following:</p>

<pre><code>var recursiveDecoded = decodeURIComponent( jQuery.param(body) );
console.log(recursiveDecoded);

meta[prune]=true&amp;meta[returnFields][]=gf&amp;meta[returnFields][]=gh&amp;meta[returnFields][]=gh&amp;meta[returnFields][]=rt&amp;meta[orient]=split&amp;indicators[0][type]=beta&amp;indicators[0][computeOn]=gf&amp;indicators[0][parameters][timeperiod]=5&amp;indicators[0][parameters][nbdevup]=2&amp;indicators[0][parameters][nbdevdn]=2&amp;indicators[0][parameters][matype]=0&amp;indicators[1][type]=alpha&amp;indicators[1][computeOn]=gf&amp;indicators[1][parameters][timeperiod]=30
</code></pre>

<p>If just using a serialized string result on the server leaves the string as the key in a key value pair:</p>

<pre><code>"query": {
    "{\"meta\":{\"prune\":true,\"returnFields\":[\"gf\",\"gh\",\"gh\",\"rt\"],\"orient\":\"split\"},\"indicators\":[{\"type\":\"beta\",\"computeOn\":\"gf\",\"parameters\":{\"timeperiod\":5,\"nbdevup\":2,\"nbdevdn\":2,\"matype\":0}},{\"type\":\"alpha\",\"computeOn\":\"gf\",\"parameters\":{\"timeperiod\":30}}]}": ""
  },
</code></pre>

<p>My backend processing is done with Python. What modules exist to convert the above result to a <code>dict</code> resembling the original object?</p>

<p>Well, since we hashed it out in the comments, I'll post the answer here for posterity.</p>

<p>Use a combination of <code>JSON.stringify</code> on the JavaScript side to serialize your data structure and <code>json.loads</code> on the Python side to deserialize it. Pass the serialized structure as a query string parameter ("query" in your example) and then read the value from that query string parameter in Python. Huzzah!</p>

<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify</a></p>

javascript jquery python json