-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.

1
  • Please provide a minimal reproducible example, as well as the current and expected output. Commented Dec 22, 2020 at 16:19

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Don't use id as variable name, as this shadows the built-in id.
Thanks @baduker! I've improved my answer!
1

values is a dict, you can direct access fields

numid = values['data']['id']
phone = values['data']['phone']

Comments

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'
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.