-1

I am calling an API and I am getting a JSON response like the one above:

{
  "data": {
    "styles": [
      {
        "groups": [
          {
            "product": [
              {
                "code": "0000001",
                "date": null
              }
            ]
          },
          {
            "product": [
              {
                "code": "0000002",
                "date": "2020-05-02T00:00:00.000Z"
              }
            ]
          },
          {
            "product": [
              {
                "code": "0000003",
                "date": "2020-04-22T00:00:00.000Z"
              }
            ]
          }
        ]
      },
      {
        "groups": [
          {
            "product": [
              {
                "code": "0000011",
                "date": "2020-05-12T00:00:00.000Z"
              }
            ]
          },
          {
            "product": [
              {
                "code": "0000012",
                "date": "2020-05-12T00:00:00.000Z"
              }
            ]
          }
        ]
      }
    ]
  }
}

I am not familiar with handling JSON and this one is super nested. All I want to do is to create a tuple (or a dict) which would look like:

(0000001, null)
(0000002, 2020-05-02T00:00:00.000Z)
(0000003, 2020-04-22T00:00:00.000Z)
(0000011, 2020-05-12T00:00:00.000Z)
(0000012, 2020-05-12T00:00:00.000Z)

Could you please advice me one how to do that?

1131
  • 407
  • 3
  • 15
  • 1
    The answer here should work for you: [link](https://stackoverflow.com/questions/19483351/converting-json-string-to-dictionary-not-list) – GinTonic Jun 02 '20 at 09:16

3 Answers3

1

You should use the json library:

import json

def json_to_dict(api_response):
    # I assume the api response is a string 
    api_response_dict = json.loads(api_response)
    return api_response_dict

This will create one dictionary with all of the contents. Then you can parse this dictionary and take only the key-value pairs that you care about:

list_of_products = []
for product_group in api_response_dict['data']['styles']:
    for product in product_group['groups']:
        code = product['product'][0]['code']
        date = product['product'][0]['date']
        list_of_products.append((code, date))

This was just an example.

geoph9
  • 357
  • 3
  • 18
1

Your question is about parsing JSON with Python.

You can perform this using json.loads :

import json
my_string = '{"foo": "bar"}'
data = json.loads(my_string)

However, it looks like you are using an API response. If your using requests (and you should), you can use

response = requests.get([...])
data = response.json()
Blusky
  • 3,470
  • 1
  • 19
  • 35
1
import json

# your work

d = json.load(xxxx) # load it.
for i in d["data"]["styles"]:
    for j in i["groups"]:
        print((j['product'][0]['code'], j['product'][0]['date']))

Result:

('0000001', None) # In python, there is no null. it will be replace to None.
('0000002', '2020-05-02T00:00:00.000Z')
('0000003', '2020-04-22T00:00:00.000Z')
('0000011', '2020-05-12T00:00:00.000Z')
('0000012', '2020-05-12T00:00:00.000Z')
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49