Getting size of JSON array and read it's elements Python
<p>I have an json array that looks like this:</p>

<pre><code>{
        "inventory": [
            {
                "Name": "katt"
            },
            {
                "Name": "dog"
            }
        ]
}
</code></pre>

<p>And now I want to access this array in a program that I'm creating and remove a element, for example "Name": "dog".
I'm not super familiar with how to work with json in python, but so far I have tried something like this:</p>

<p>import json</p>

<pre><code>jsonfile = open("viktor.json", "r")
jsonObj = json.load(jsonfile)

jsonfile.close()

counter = 0
for item in range(len(jsonObj["inventory"])):
    print(jsonObj["inventory"][counter])
    print(type(jsonObj["inventory"][counter]))
    if jsonObj["inventory"][counter] == argOne:
        print("hej")
counter += 1
</code></pre>

<p>So first I read from the json and stores the data in a variable.
Then I want to loop through the whole variable and see if I can find any match, and if so, I want to remove it. I think I can use a pop() method here or something?
But I can't seem to get my if-statement to work properly since the jsonObj["inventory"][counter] is a dict and argOne is a string.</p>

<p>What can I do instead of this? Or what I'm missing?</p>

<p>You can use <a href="https://docs.python.org/3/library/functions.html#filter" rel="nofollow">filter</a>:</p>

<pre><code>In [11]: import json

In [12]: with open("viktor.json", "r") as f:
    ...:     jsonObj = json.load(f)
    ...:     

In [13]: argOne = 'katt' #Let's say

In [14]: jsonObj['inventory'] = list(filter(lambda x: x['Name'] != argOne, jsonObj['inventory']))

In [15]: jsonObj
Out[15]: {'inventory': [{'Name': 'dog'}]}
</code></pre>

python python-3.x