NachoMiguel
NachoMiguel

Reputation: 993

Getting a dict from a list Python

I have a JSON that looks like this:

"payment_methods": [{
    "payment_type": "CC",
    "cardSubType": "AX",
    "card_number": "377777684182882",
    "expiration_month": "01",
    "expiration_year": "2018",
    "security_code": "",
    "cvv2": "",
    "name": {
        "first_name": "John",
        "last_name": "Smith"
    },
    "billing_address": {
        "country": "United States",
        "address_line1": "666 SUNSET BLVD",
        "address_line2": null,
        "city": "Anaheim",
        "state": "California",
        "zip_code": "92808",
        "phone": "0123456789"
    },
    "payment_amount": "",
    "payment_percentage": ""
}]

What need to do is the save the billing_address into a variable to later use the values inside that dict. Would billing_address in payment_methods work?

Is there a better/pythonic way to do this?

Upvotes: 1

Views: 109

Answers (3)

mhawke
mhawke

Reputation: 87084

You have a JSON string, so parse it into a Python dictionary. The (almost valid) string represents a dictionary, but it needs to be enclosed in { and }. On the assumption that the json string is returned from some other API, this can be done with format() like this:

json_string = '{{{}}}'.format('''"payment_methods": [{
    "payment_type": "CC",
    "cardSubType": "AX",
    "card_number": "377777684182882",
    "expiration_month": "01",
    "expiration_year": "2018",
    "security_code": "",
    "cvv2": "",
    "name": {
        "first_name": "John",
        "last_name": "Smith"
    },
    "billing_address": {
        "country": "United States",
        "address_line1": "666 SUNSET BLVD",
        "address_line2": null,
        "city": "Anaheim",
        "state": "California",
        "zip_code": "92808",
        "phone": "0123456789"
    },
    "payment_amount": "",
    "payment_percentage": ""
}]''')

Now the string can be parsed into a dictionary using the json module, and the billing address details by accessing the appropriate keys and list indexes within the dictionary. Assuming you want only the billing details for the first payment method in the list of (potentially many) payment methods:

import json
from pprint import pprint

payment_methods = json.loads(json_string)['payment_methods']
billing_address = payment_methods[0]['billing_address']

>>> pprint(billing_address)
{u'address_line1': u'666 SUNSET BLVD',
 u'address_line2': None,
 u'city': u'Anaheim',
 u'country': u'United States',
 u'phone': u'0123456789',
 u'state': u'California',
 u'zip_code': u'92808'}

Upvotes: 0

lapinkoira
lapinkoira

Reputation: 8988

So you want to obtain billing_address property?

import json

#Fix the null, normalize the json since is not a valid json
null = None
data = json.dumps({"payment_methods": [{
    "payment_type": "CC",
    "cardSubType": "AX",
    "card_number": "377777684182882",
    "expiration_month": "01",
    "expiration_year": "2018",
    "security_code": "",
    "cvv2": "",
    "name": {
        "first_name": "John",
        "last_name": "Smith"
    },
    "billing_address": {
        "country": "United States",
        "address_line1": "666 SUNSET BLVD",
        "address_line2": null,
        "city": "Anaheim",
        "state": "California",
        "zip_code": "92808",
        "phone": "0123456789"
    },
    "payment_amount": "",
    "payment_percentage": ""
}]})

data = json.loads(data)

billing_address = data["payment_methods"][0]["billing_address"]

print billing_address

Upvotes: 3

Joe T. Boka
Joe T. Boka

Reputation: 6581

I would separate these into 2 dictionaries. The second dictionary, with the address, would be stored in the variable billing_address

It would look like this:

payment_methods = {
                  "payment_type": "CC",
                  "cardSubType": "AX",
                  "card_number": "377777684182882",
                  "expiration_month": "01",
                  "expiration_year": "2018",
                  "security_code": "",
                  "cvv2": "",
                  "first_name": "John",
                  "last_name": "Smith",
                  "payment_amount": "",
                  "payment_percentage": ""
                  }
billing_address = {
                      "country": "United States",
                      "address_line1": "666 SUNSET BLVD",
                      "address_line2": "null",
                      "city": "Anaheim",
                      "state": "California",
                      "zip_code": "92808",
                      "phone": "0123456789"
                    }

Upvotes: 0

Related Questions