-2

I have a JSON value and would like to retrieve each value in it.

{"profile": [{"user_id": "91609430", "user_phone": "6525295", "user_type": "panger", "user_tier": "silver", "user_name": "Robert Sambuena", "user_email": "sauena.@gmail.com"}], "is_from_hc": false, "errors": [{"msg": "Driver Profile Not Found", "code": 404, "key": "profile.driver"}], "soundwaves": {"audio_messages": ["hc-education"], "access_token": "9f7fd8f060f2dc2fa", "has_context": false, "queue": "", "iso_language": "", "context": {"user_id": "", "user_tier": "", "user_name": "", "user_phone": "", "is_from_hc": false, "user_type": "", "has_context": false, "zendesk_language": "", "taxtype": "", "scenario_name": "", "zen_country": "", "booking_code": ""}}}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
art
  • 313
  • 2
  • 5
  • 19

2 Answers2

1

You can use Python built-in json lib for this task:

import json

json_str = '{"profile": [{"user_id": "91609430", "user_phone": "6525295", "user_type": "panger", "user_tier": "silver", "user_name": "Robert Sambuena", "user_email": "sauena.@gmail.com"}], "is_from_hc": false, "errors": [{"msg": "Driver Profile Not Found", "code": 404, "key": "profile.driver"}], "soundwaves": {"audio_messages": ["hc-education"], "access_token": "9f7fd8f060f2dc2fa", "has_context": false, "queue": "", "iso_language": "", "context": {"user_id": "", "user_tier": "", "user_name": "", "user_phone": "", "is_from_hc": false, "user_type": "", "has_context": false, "zendesk_language": "", "taxtype": "", "scenario_name": "", "zen_country": "", "booking_code": ""}}}'
json_obj = json.loads(json_str)

# Now you can iterate over the json_obj as you would do with any dict:
for k, v in json_obj.items():
    print(k, v)
mahmoudajawad
  • 105
  • 2
  • 10
  • hi. thank you so much for the help. I only need this value [{u'user_id': u'91609430', u'user_phone': u'658402', u'user_type': u'passenger', u'user_tier': u'silver', u'user_name': u'Robert', u'user_email': u'sambu@gmail.com'}] how i can retrieve each of them? – art Feb 05 '20 at 04:36
  • have your tried to copy and paste my sample and play with it, @art? – mahmoudajawad Feb 08 '20 at 15:09
0

Using yaml lib in python: (Answer updated as per ur need)

import yaml

json_str = '{"profile": [{"user_id": "91609430", "user_phone": "6525295", "user_type": "panger", "user_tier": "silver", "user_name": "Robert Sambuena", "user_email": "sauena.@gmail.com"}], "is_from_hc": false, "errors": [{"msg": "Driver Profile Not Found", "code": 404, "key": "profile.driver"}], "soundwaves": {"audio_messages": ["hc-education"], "access_token": "9f7fd8f060f2dc2fa", "has_context": false, "queue": "", "iso_language": "", "context": {"user_id": "", "user_tier": "", "user_name": "", "user_phone": "", "is_from_hc": false, "user_type": "", "has_context": false, "zendesk_language": "", "taxtype": "", "scenario_name": "", "zen_country": "", "booking_code": ""}}}'
json_obj = yaml.load(json_str)
# Retrive the Profile Value like this
profile = json_obj['profile']
print(profile)
# Iterate to get each value in profile
for i in profile:
    for j in i:
        print(j," : ", i[j])
smack cherry
  • 471
  • 3
  • 7