-1

So I have been playing around with JSONand been trying to print out but without any luck. Basically what JSONI am trying to print out is the cityplace as you will see here:

{
  'id': '5235246c-ac21a7-151128-8cd9-512512',
  'type': 'Hello world',
  'metadata': {
    'invite_text': "A very cool text here!!",
    'cityplace': [
      {
        'display_text': 'Stackoverflow City',
      }
    ]
  }
}

What I have done for code is that I made a loop that prints out by calling the name test_print which gives me the JSON object that I printed out.

What I did try was

print(test_print['metadata']['cityplace']['display_text'])

unfortunately this give me error

TypeError: list indices must be integers or slices, not str

So what I needed to do is:

print(test_print['metadata']['cityplace'][0]['display_text'])

and now my question is: Is it possible to print out without needing to add [0] because it might not always be 0 in the future?

8
  • print(test_print['metadata']['cityplace'][0]['display_text']) ? Commented Sep 5, 2018 at 9:30
  • 1
    I don't really understand what you're asking. That's a list, if you want to print out the first item you need to use [0], if you want to print every item you can iterate through with a loop Commented Sep 5, 2018 at 9:30
  • @Rakesh Yes, I did printed out saying that it worked but my question is, if its possible to make it without adding the [0] ? Commented Sep 5, 2018 at 9:30
  • @DanielRoseman How can I make it loop in that case? I don't know how to check how long the list is? Or..? Commented Sep 5, 2018 at 9:31
  • 2
    Why do you need to check how long it is? Just do for item in test_print['metadata']['cityplace']: print(item['display_text']). Commented Sep 5, 2018 at 9:33

3 Answers 3

1

cityplace is a list. Use Index.

Ex:

test_print = {
  'id': '5235246c-ac21a7-151128-8cd9-512512',
  'type': 'Hello world',
  'metadata': {
    'invite_text': "A very cool text here!!",
    'cityplace': [
      {
        'display_text': 'Stackoverflow City',
      }
    ]
  }
}

print(test_print['metadata']['cityplace'][0]['display_text'])

Or iterate it.

Ex:

for i in test_print['metadata']['cityplace']:
    print(i["display_text"])
Sign up to request clarification or add additional context in comments.

Comments

1

You cityplace is not a JSON. It is a list containing only one JSON. So, you have to pick the JSON from it, which is, of course, [0]. If the list is gonna have more elements in the future, you will need to iterate

 texts = [city['display_text'] for city in test_print['metadata']['cityplace']

Comments

1

Since you are using indexed array , the only way to access the array values is using index of the array (and will be always 0 - doesn't change in future).

You can use associative array, in order to access the access key and its value.

This link may help you in creating associative array.

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.