json recursive comparison with another json in python
<p>I am comparing if all elements of a json object are inside another json.
for that i grab all keys in one layer of the small one and check if they are in the big one and if they are the same. to go into a deeper layer i call the function giving it the element that contains the deeper layer. (in this funtion now i detect the deeper layer with the key "results" but i futur it will be changed to a variable key.
my Problem is that i can't call the recursive function. it sais "string indices must be integers, not unicode" in the script when trying to call the function in the function.</p>

<pre><code>def compareJson(example_json_s, target_json_s): 
    #parsed_json
    example_json = example_json_s
    target_json = target_json_s
    p = 0
    keys = []
    for key in example_json.keys(): 
        keys.insert(p, key)
        p = p + 1

    passed = 0;
    for x in keys: 
        print "Keys"
        if x != "results": 
            if not x in target_json or x not in example_json.keys() and not example_json[x] == target_json[x]: 
                passed = 1
        else:
            print """###inhabits "results " going one layer deeper"""
        compareJson(example_json[key], target_json[key])
    return passed
</code></pre>

<p>and some example json objects:</p>

<pre><code>{
    "results": {
        "clock": true,
        "update": false,
        "autoreg": false
    }
} 

{
    "id": "1523",
    "dlid": "009029",
    "serial": "1017030022",
    "statuscode": 128,
    "results": {
        "event": true,
        "counter": true,
        "clock": true,
        "eqep": true,
        "usb": false,
        "modem": true,
        "sim": true,
        "pin": true,
        "encrypt": false,
        "upload": true,
        "update": true,
        "autoreg": true
    },
    "ok": false,
    "confirmed": false
}
</code></pre>

<p>And the Error code here:</p>

<pre><code>Traceback (most recent call last):
  File "/home/philip/Desktop/empfang_test.py", line 199, in &lt;module&gt;
    empfange() #Programm wartet in einer Endlosschleife auf eingehende Nachrichten.
  File "/home/philip/Desktop/empfang_test.py", line 193, in empfange
    checkvalue=compareJson(json.loads(config[1][1]),parsed_json_dummy)
  File "/home/philip/Desktop/empfang_test.py", line 183, in compareJson
    compareJson(example_json[key],target_json[key])   
TypeError: string indices must be integers, not str
</code></pre>