Python json discover null value

Asked

Viewed 79 times

0

Hello.

I’m having trouble checking null value in list I have a json, whose section that gives me trouble is below:

{
    "id": "200827",
    "nome": "Emporio",
    "idPai": null,
    "nomePai": null
},

I understood that if you make the following command:

if lista[linha][coluna]:

the following code is only executed if the value is not null, but that is not what happens to me. I’m trying to run this snippet of code that doesn’t answer like this.

You should only enter the block if the value of the first column is not null. In this case, I test the value of the third column and only interested in cases of null value, however the if clause never returns false, although in the fifth iteration it should fall in Else, with the print list at the end.

if original[i][0]:
    if original[i][2]:
        print (original[i][2])
    else:
        executa tudo o que me interessa
        Jamais entra nesta parte do código

Output from Print:

['200828']
['200827']
['200769']
['200769']
[None]

I’m afraid you’re making some very basic mistake, but I’m not quite sure what it would be.

  • Alano, can you leave the rest of the code in question ? I don’t think it is possible to answer it only with what you have put.

  • it was possible to answer yes, but only because it has the printed output of that print there. Otherwise I would have to have the code that reads the json, and a real example of how is this json there.

  • but yes, what is happening, s and the JSON is this right there, is that it is being read wrong into Python. Using a number to access the value instead of the keys "id", "nome", ... suggests that the data is not being imported into Python in the correct way.

  • jsbueno already solved the problem. As for the use of indexes instead of key names, I wanted to make the code more generic. But I’ve rethought.

1 answer

0


Its ultimate value, what is in original[i][0] is not yet null (None in Python) - as you can see by what is printed. What is printed is a list, with a single element, that it is the None. The value of original[i][0] is a list that has at least one element - which, in Python tests, has value True.

If the structure is always this, just change your test to:

if original[i][0]:
    if original[i][2][0]:
        print (original[i][2])
    else:
        # executa tudo o que me interessa

Browser other questions tagged

You are not signed in. Login or sign up in order to post.