0

I'm pretty new to Python so I'm only just starting to work with API's. I have retrieved the data I need from an API and it returns in the following format:

{u'id': u'000123', u'payload': u"{'account_title': u’sam b’, 'phone_num': ‘1234567890’, 'security_pin': u'000000', 'remote_word': u’secret123’, 'email_address': ‘email@gmail.com’, 'password': u’password123’}”}

So when I print my variable, it returns the above...

If I just want part of what it returns, how do I go about writing this? I was able to return id without issue, and if I specified 'payload' it returned everything after that. It seems like account_title, phone_num, security_pin, remote_word, email_address and password are all nested inside of 'payload'

Would would the best way be to have a variable, when printed, return just the email_address for example?

Thanks!

srb7
  • 337
  • 2
  • 9
  • Your question is unclear. That JSON is already parsed as a Python dictionary. So it works just like any other dictionary. And the "payload" key itself contains another dictionary, which *also* just works like a normal dictionary. – Daniel Roseman Sep 09 '19 at 11:16

1 Answers1

1

Welcome to Python! Sounds like you're getting right into it. It would be best to begin reading fundamentals, specifically about the Dictionary Data Structure

The Dictionary, or dict is what you are referencing in your question. It's a key-value store that is generally[1] un-ordered. The dict is a great way to represent JSON data.

Now you are asking how to extract information from a dictionary. Well, you seem to have it working out thus far! Let's use your example:

d = {u'id': u'000123', u'payload': u"{'account_title': u’sam b’, 'phone_num': ‘1234567890’, 'security_pin': u'000000', 'remote_word': u’secret123’, 'email_address': ‘email@gmail.com’, 'password': u’password123’}"}

Now if we write d['id'], we'll get the id (which is 000123)

If we write d['payload'], we'll get the dictionary within this larger dictionary. Cool part about dicts, they can be nested like this! As many times as you need.

d['payload']

"{'account_title': u’sam b’, 'phone_num': ‘1234567890’, 'security_pin': u'000000', 'remote_word': u’secret123’, 'email_address': ‘email@gmail.com’, 'password': u’password123’}"

Then as per your question, if you wanted to get email, it's the same syntax and you're just nesting the accessor. Like so:

d['payload']['email_address']

Hope that helps!

  1. For the longest time, dicts were un-ordered in Python. In versions 3.6 and up, things began changing. This answer provides great detail on that. Otherwise, prior to that, using collections.OrderedDict was the only way to get a dict ordered by insertion-order
Bartek
  • 15,269
  • 2
  • 58
  • 65
  • When I try the 2 square bracketed values next to each other like in your example, I get this error `TypeError: string indices must be integers` - any ideas? – srb7 Sep 09 '19 at 11:36
  • @samrb Sounds like you're attempting to access a list like a dictionary. Are you sure you're using the same data as you presented in your question? – Bartek Sep 09 '19 at 12:49