[Solved] How to iterate over JSON array? [closed]


Use the json package and load the json. Assuming it is a string in memory (as opposed to a .json file):

jsonstring = """
[
  {
  "issuer_ca_id": 16418,
  "issuer_name": "C=US, O=Let's Encrypt, CN=Let's Encrypt Authority X3",
  "name_value": "sub.test.com",
  "min_cert_id": 325717795,
  "min_entry_timestamp": "2018-02-08T16:47:39.089",
  "not_before": "2018-02-08T15:47:39"
  },
{
"issuer_ca_id":9324,
"issuer_name":"C=US, O=Amazon, OU=Server CA 1B, CN=Amazon",
"name_value":"marketplace.test.com",
"min_cert_id":921763659,
"min_entry_timestamp":"2018-11-05T19:36:18.593",
"not_before":"2018-10-31T00:00:00",
"not_after":"2019-11-30T12:00:00"
}
]"""

import json
j = json.loads(jsonstring)
[item["issuer_name"] for item in j]

Gives:

["C=US, O=Let's Encrypt, CN=Let's Encrypt Authority X3",
 'C=US, O=Amazon, OU=Server CA 1B, CN=Amazon']

Now, these don’t look like names to me, but that’s what is assigned to the issuer_name field, so I think that’s something you have to take up with the owner of the data.

If it’s a file, you do the loading in this basic pattern:

# something like this
with open("jsonfile.json", "rb") as fp:
    j = json.load(fp) 

See the docs here: https://docs.python.org/3.7/library/json.html

2

solved How to iterate over JSON array? [closed]