-6

I have this JSON array with multiple roots:

[
  {
  "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"
}
]

I want to iterate over it and print issuer_name values in Python. Any solution, please?

martineau
  • 119,623
  • 25
  • 170
  • 301
W0rm
  • 25
  • 1
  • 6
  • I think the title of this question is misleading and it is likely a dup of [How to find a particular json value by key?](https://stackoverflow.com/questions/14048948/how-to-find-a-particular-json-value-by-key). – martineau Nov 11 '18 at 20:44

1 Answers1

0

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

Charles Landau
  • 4,187
  • 1
  • 8
  • 24