can't print the value of json object in django
<p>I have this json object in ajax_data variable</p>

<pre><code>{
    "columns[0][data]": "0", 
    "columns[1][name]": "", 
    "columns[5][searchable]": "true", 
    "columns[5][name]": "", 
    "columns[4][search][regex]": "false", 
    "order[0][dir]": "asc", 
    "length": "10", 
}
</code></pre>

<p>I have converted it using json.loads() function like.</p>

<pre><code>ajax_data = json.loads(ajax_data)
</code></pre>

<p>I want to get the value if "order[0][dir]" and "columns[0][data]" but if i print it using </p>

<pre><code>ajax_data['order'][0]['dir]
</code></pre>

<p>its giving error :</p>

<pre><code>KeyError at /admin/help
'order'
</code></pre>

<p>But same code works if i access it for length key then it works.</p>

<p>The keys you have used are actually not a good way of implementation. </p>

<pre><code>{
    "columns[0][data]": "0", 
    "columns[1][name]": "", 
    "columns[5][searchable]": "true", 
    "columns[5][name]": "", 
    "columns[4][search][regex]": "false", 
    "order[0][dir]": "asc", 
    "length": "10", 
}
</code></pre>

<p>Instead of this you should hav gone for </p>

<pre><code>{
    "columns": [
        {"data": "0", "name": "", "searchable": "true", "name": "", "search": {
            "regex": "false"}
        },
        {"data": "0", "name": "", "searchable": "true", "name": ""," search": {
            "regex": "false"}},
        {"data": "0", "name": "", "searchable": "true", "name": "", "search": {
            "regex": "false"}},
        {"data": "0", "name": "", "searchable": "true", "name": "", "search": {
            "regex": "false"}},
        {"data": "0", "name": "", "searchable": "true", "name": "", "search": {
            "regex": "false"}},
        {"data": "0", "name": "", "searchable": "true", "name": "", "search": {
            "regex": "false"}},
    ],
    "order": [
        {"dir": "asc"}
    ],
    "length": "10"
}
</code></pre>

<p>In this case ajax_data['order'][0]['dir] will result in value "asc"</p>

<p>For your current implementation the key is "order[0][dir]"</p>

<p>That is go for </p>

<p><code>ajax_data["order[0][dir]"]</code></p>

<p>Hope you understood the issue. </p>

<p>Structuring of json is very important when dealing with APIs. Try to restructure your json which will help for future too.</p>

python django