简体   繁体   中英

Accessing list within a dictionary via Python from a JSON file

I am new to Python and could use some help, please. I'm trying to use Python to read from my JSON file and get the values of the list within in a dictionary.

Once I read the JSON into my program, I have:

request body = {
  "providerName": "ProviderNameXYZ",
  "rateRequestPayments": [
    {
      "amount": 128.0,
      "creditorProfileId": "7539903942457444658",
      "debtorProfileId": "2072612712266192555",
      "paymentMethodId": "2646780961603748694016",
      "currency": "EUR",
      "userReference": "INVALID user ref automation single",
      "reconciliationId": "343546578753349"
    },
    {
      "amount": 129.0,
      "creditorProfileId": "7539903942457444658",
      "debtorProfileId": "2072612712266192555",
      "paymentMethodId": "2646780961603748694016",
      "currency": "EUR",
      "userReference": "INVALID user ref automation single",
      "reconciliationId": "343546578753340"
    }
  ]
}
  1. I now want to be able to grab the value of any particular key. I've tried accessing it via several routes:
  • rateRequestPayments[0].amount
  • rateRequestPayments()[0].amount
  • for k, v in request_body.rateRequestPayments(): print(k, v)
  • for each in request_body.rateRequestPayments: print(each('amount'))
  • values = eval(request_body['rateRequestPayments']) print(values[0]) All of these end up with errors. Per the 2nd comment below: request_body['rateRequestPayments'][0]['amount'] This works!
  1. I also want to be able to delete the whole key-value pair ("amount": 128.0,) from the request_body. request_body['rateRequestPayments'][0]['amount'] does not work for this. Not sure how to reference this.

I know I am missing something simple but I'm just unsure what it is. Any help would be greatly appreciated.

Python is great for data science, and has many features that make it fit for the job. One of those features is finding data in a data set. body['rateRequestPayments'][<!WHICH DATA SET YOU WANT TO ACCESS>][<!VALUE YOU WANT TO FIND>]

First you'll need to put an underscore between requests and body so its requests_body = {...} .

To access a key-value pair:

requests_body["rateRequestPayments"] #to access the list of two dicts
requests_body["rateRequestPayments"][0] #to access the individual dicts by index
requests_body["rateRequestPayments"][0]["amount"] #to access the value of a particular key (in this case "amount" is the key)

To delete a key value pair, just use the del statement:

del requests_body["rateRequestPayments"][0]["amount"]

This will delete the key-value pair in the variable requests_body , so trying to access this key again will raise a KeyError error.

To access and delete a key-value pair, use .pop() :

value = requests_body["rateRequestPayments"][0].pop("amount")
#value is now 128.0 and the key-value pair is deleted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM