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>Making the change suggested by @arvindpdmn (to be more pythonic).</p>

<pre><code>for index, item in enumerate(jsonObj["inventory"]):
    print(item)
    print(type(item))  # Here we have item is a dict object
    if item['Name'] == argOne:  # So we can access their elements using item['key'] syntax
        print(index, "Match found")
</code></pre>

<p>The <code>for</code> loop is responsible to go through the array, which contains <code>dict</code> objects, and for each <code>dict</code> it will create a <code>item</code> variable that we use to try to get a match.</p>

<p><strong>edit</strong>
In order to remove the element if it is in the list, I suggest you to use this:</p>

<pre><code>new_list = []
for item in jsonObj["inventory"]:
    if item['Name'] is not argOne:  # add the item if it does not match
        new_list.append(item)
</code></pre>

<p>This way you will end with the list you want (new_list).</p>

<pre><code># Or shorter.. and more pythonic with comprehensions lists.
new_list = [item for item in jsonObj['inventory'] if item['Name'] is not argOne]
</code></pre>

python python-3.x