P
P
parmay2018-08-27 19:32:52
Python
parmay, 2018-08-27 19:32:52

How to display the required variable from JSON?

Good evening!
Can you please tell me how to extract the required variable from json?
Python script using api qiwi on request displays a json report received from qiwi. Its content is as follows:

{
  "accounts": [
    {
      "alias": "string",
      "balance": {
        "amount": 0,
        "currency": 0
      },
      "bankAlias": "string",
      "currency": 0,
      "defaultAccount": true,
      "fsAlias": "string",
      "hasBalance": true,
      "title": "string",
      "type": {
        "id": "string",
        "title": "string"
      }
    }
  ]
}

It is necessary to print the value of the amount variable. Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2018-08-27
@parmay

Something like this...

import json
myjson = """
{
  "accounts": [
    {
      "alias": "string",
      "balance": {
        "amount": 0,
        "currency": 0
      },
      "bankAlias": "string",
      "currency": 0,
      "defaultAccount": true,
      "fsAlias": "string",
      "hasBalance": true,
      "title": "string",
      "type": {
        "id": "string",
        "title": "string"
      }
    }
  ]
}
"""
data = json.loads(myjson)
print data["accounts"][0]["balance"]["amount"]

Well, to read - https://realpython.com/python-json/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question