-1

I am trying to check if a specific item in a json file is equal to one of my python variables.

{'data': {'redemption': {'channel_id': 'secret',
                         'id': 'secret',
                         'redeemed_at': '2021-02-08T09:46:22.637059711Z',
                         'reward': {'background_color': '#FA1ED2',
                                    'channel_id': '145998001',
                                    'cooldown_expires_at': None,
                                    'cost': 500,
                                    'default_image': {'url_1x': 'https://static-cdn.jtvnw.net/custom-reward-images/ghost-1.png',
                                                      'url_2x': 'https://static-cdn.jtvnw.net/custom-reward-images/ghost-2.png',
                                                      'url_4x': 'https://static-cdn.jtvnw.net/custom-reward-images/ghost-4.png'},
                                    'global_cooldown': {'global_cooldown_seconds': 1,
                                                        'is_enabled': False},
                                    'id': '123',
                                    'image': None,
                                    'is_enabled': True,
                                    'is_in_stock': True,
                                    'is_paused': False,
                                    'is_sub_only': False,
                                    'is_user_input_required': False,
                                    'max_per_stream': {'is_enabled': False,
                                                       'max_per_stream': 1},
                                    'max_per_user_per_stream': {'is_enabled': False,
                                                                'max_per_user_per_stream': 1},
                                    'prompt': '*Dabs*',
                                    'redemptions_redeemed_current_stream': None,
                                    'should_redemptions_skip_request_queue': False,
                                    'template_id': 'template:4425c37e-6881-442a-aa3d-fdc6998a29de',
                                    'title': 'Dab!',
                                    'updated_for_indicator_at': '2020-09-10T18:55:40.064177881Z'},
                         'status': 'UNFULFILLED',
                         'user': {'display_name': 'Androteex',
                                  'id': 'secret',
                                  'login': 'androteex'}},
          'timestamp': '2021-02-08T09:46:22.637059711Z'},
 'type': 'reward-redeemed'}

I want to find the second id: 'id': '123' and check if id is equal to 123. And if so I want to print that string. How could I do that?

2

2 Answers 2

1

You can use the JSON module.

import json

data = json.loads(my_json)
my_id = data['data']['redemption']['reward']['id']
if my_id == '123':
    print(data)
Sign up to request clarification or add additional context in comments.

1 Comment

although maybe not use id as the var name since its a built in function <built-in function id>
0

Granted it was added to data (can be done with json.loads):

id = data['data']['redemption']['reward']['id']
idcheck = 123
if (int(id) == idcheck):
    print ("YES")

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.