-1

I am trying to get the "id" and "phone" values in the data out of this json:

  "status": "success",
  "data": {
    "id": "MertD43fdfdDdsjk",
    "countrycode": "GB",
    "country": "United Kingdom",
    "createdAt": 15534534000,
    "validUntil": 15534534000,
    "service": "gmail",
    "phone": "446576879809",
    "price": 0.21
  }
}

My current code is

json_data = json.loads(response.text)
    #print(json_data)
    values = json_data.items()
    #print(values)
    recieved = False


    for key, value in values:
        print(key, value)

        if key == "id":
            numid = value
            print(numid)
        elif key == "phone":
            phone = value
            print("Recieved the Phone Number - ",phone)
            recieved = True
        elif value == "Mobile number isn't received. Please try again":
            if recieved == False:
                getPhoneNumber() 

I know I am only accessing status and data items and not the stuff inside of data. Currently, I am receiving the JSON as a result of requesting from an API so the values are always different.

Zach C
  • 71
  • 1
  • 10

4 Answers4

4

If that's a pure JSON from an API, why not do this?

d = {
    "status": "success",
    "data": {
        "id": "MertD43fdfdDdsjk",
        "countrycode": "GB",
        "country": "United Kingdom",
        "createdAt": 15534534000,
        "validUntil": 15534534000,
        "service": "gmail",
        "phone": "446576879809",
        "price": 0.21
    }
}

print(d["data"]["id"], d["data"]["phone"])

Output:

MertD43fdfdDdsjk 446576879809
baduker
  • 19,152
  • 9
  • 33
  • 56
1

No need for loops, you can access the data by the key;

import json

json_string = '{"status": "success", "data": {"id": "MertD43fdfdDdsjk", "countrycode": "GB", "country": "United Kingdom", "createdAt": 15534534000, "validUntil": 15534534000, "service": "gmail", "phone": "446576879809", "price": 0.21 } }'
json_data = json.loads(json_string)
data_dict = json_data['data']

dataid = data_dict['id']
phone = data_dict['phone']

print(dataid)
print(phone)

MertD43fdfdDdsjk

446576879809

Getting values from JSON using Python

0stone0
  • 34,288
  • 4
  • 39
  • 64
1

values is a dict, you can direct access fields

numid = values['data']['id']
phone = values['data']['phone']
shepx
  • 68
  • 4
0
  1. Use requests module which provides more features of extracting text # pip install requests

  2. In this module after getting a response try response.json

  3. For eg: if the received data is

     d = {
         "status": "success",
         "data": {
             "id": "MertD43fdfdDdsjk",
             "countrycode": "GB",
             "country": "United Kingdom",
             "createdAt": 15534534000,
             "validUntil": 15534534000,
             "service": "gmail",
             "phone": "446576879809",
             "price": 0.21
         }
     }
    

    Then to access id and phone number:=>

     data['data']['id'] # or 'phone'
    
Nanthakumar J J
  • 860
  • 1
  • 7
  • 22