Home > database >  Python How to Iterate through the nested json dictionaries and print only selected value
Python How to Iterate through the nested json dictionaries and print only selected value

Time:08-27

From this JSON dictionary of 10 claims I want to print only the text, title, url and TextualRating. I am a bit confused due to the nested dictionaries.

How do I loop through all of these and print only the selected parameters our for them all them out (text, title, url and TextualRating)

Photos From Json viewer: Here is the Json beautifiedphoto

Json Code:

'''{'claims': [{'text': 'Apple AirPods destroy your health, emit 5 times more radiation than over-ear headphones and emit "more radiation than a microwave"', 'claimant': 'Social media users', 'claimDate': '2022-05-01T00:00:00Z', 'claimReview': [{'publisher': {'name': 'USA Today', 'site': 'usatoday.com'}, 'url': 'https://www.usatoday.com/story/news/factcheck/2022/08/10/fact-check-context-missing-claim-health-effects-airpods/9661778002/', 'title': 'Fact check: Claim about health effects of AirPods and other Bluetooth earphones is missing context', 'reviewDate': '2022-08-11T01:33:14Z', 'textualRating': 'Missing Context', 'languageCode': 'en'}]}, {'text': 'Apple Logo Alan Turing Death', 'claimant': 'facebook.com', 'claimDate': '2022-07-01T20:40:00Z', 'claimReview': [{'publisher': {'name': 'Lead Stories', 'site': 'leadstories.com'}, 'url': 'https://leadstories.com/hoax-alert/2022/07/fact-check-apple-logo-is-not-honoring-inventor-alan-turing-who-may-have-died-by-poisoned-apple.html', 'title': 'Fact Check: Apple Logo Is NOT Honoring Inventor Alan Turing Who May Have Died By Poisoned Apple', 'reviewDate': '2022-07-01T20:40:00Z', 'textualRating': 'Urban Legend', 'languageCode': 'en'}]}, {'text': 'Radiation from Apple AirPods dangerous', 'claimant': 'Social media user', 'claimReview': [{'publisher': {'name': 'THIP Media', 'site': 'thip.media'}, 'url': 'https://www.thip.media/health-news-fact-check/fact-check-is-radiation-from-apple-airpods-dangerous/30863/', 'title': 'Fact Check: Is radiation from Apple AirPods dangerous?', 'reviewDate': '2022-05-04T00:00:00Z', 'textualRating': 'Mostly False', 'languageCode': 'en'}]}], 'nextPageToken': 'CAM'}'''

CodePudding user response:

I am hoping your json structure is as follows,

{'claims': [{
                       'text': 'Apple AirPods destroy your health, emit 5 times more radiation than over-ear headphones and emit "more radiation than a microwave"',
                       'claimant': 'Social media users', 'claimDate': '2022-05-01T00:00:00Z', 'claimReview': [
        {'publisher': {'name': 'USA Today', 'site': 'usatoday.com'},
         'url': 'https://www.usatoday.com/story/news/factcheck/2022/08/10/fact-check-context-missing-claim-health-effects-airpods/9661778002/',
         'title': 'Fact check: Claim about health effects of AirPods and other Bluetooth earphones is missing context',
         'reviewDate': '2022-08-11T01:33:14Z', 'textualRating': 'Missing Context', 'languageCode': 'en'}]},
                   {'text': 'Apple Logo Alan Turing Death', 'claimant': 'facebook.com',
                    'claimDate': '2022-07-01T20:40:00Z', 'claimReview': [
                       {'publisher': {'name': 'Lead Stories', 'site': 'leadstories.com'},
                        'url': 'https://leadstories.com/hoax-alert/2022/07/fact-check-apple-logo-is-not-honoring-inventor-alan-turing-who-may-have-died-by-poisoned-apple.html',
                        'title': 'Fact Check: Apple Logo Is NOT Honoring Inventor Alan Turing Who May Have Died By Poisoned Apple',
                        'reviewDate': '2022-07-01T20:40:00Z', 'textualRating': 'Urban Legend', 'languageCode': 'en'}]},
                   {'text': 'Radiation from Apple AirPods dangerous', 'claimant': 'Social media user', 'claimReview': [
                       {'publisher': {'name': 'THIP Media', 'site': 'thip.media'},
                        'url': 'https://www.thip.media/health-news-fact-check/fact-check-is-radiation-from-apple-airpods-dangerous/30863/',
                        'title': 'Fact Check: Is radiation from Apple AirPods dangerous?',
                        'reviewDate': '2022-05-04T00:00:00Z', 'textualRating': 'Mostly False', 'languageCode': 'en'}]}],
        'nextPageToken': 'CAM'}

In this case, you can use the following code to extract your values form keys,

claims_json_data = {'claims': [{
                       'text': 'Apple AirPods destroy your health, emit 5 times more radiation than over-ear headphones and emit "more radiation than a microwave"',
                       'claimant': 'Social media users', 'claimDate': '2022-05-01T00:00:00Z', 'claimReview': [
        {'publisher': {'name': 'USA Today', 'site': 'usatoday.com'},
         'url': 'https://www.usatoday.com/story/news/factcheck/2022/08/10/fact-check-context-missing-claim-health-effects-airpods/9661778002/',
         'title': 'Fact check: Claim about health effects of AirPods and other Bluetooth earphones is missing context',
         'reviewDate': '2022-08-11T01:33:14Z', 'textualRating': 'Missing Context', 'languageCode': 'en'}]},
                   {'text': 'Apple Logo Alan Turing Death', 'claimant': 'facebook.com',
                    'claimDate': '2022-07-01T20:40:00Z', 'claimReview': [
                       {'publisher': {'name': 'Lead Stories', 'site': 'leadstories.com'},
                        'url': 'https://leadstories.com/hoax-alert/2022/07/fact-check-apple-logo-is-not-honoring-inventor-alan-turing-who-may-have-died-by-poisoned-apple.html',
                        'title': 'Fact Check: Apple Logo Is NOT Honoring Inventor Alan Turing Who May Have Died By Poisoned Apple',
                        'reviewDate': '2022-07-01T20:40:00Z', 'textualRating': 'Urban Legend', 'languageCode': 'en'}]},
                   {'text': 'Radiation from Apple AirPods dangerous', 'claimant': 'Social media user', 'claimReview': [
                       {'publisher': {'name': 'THIP Media', 'site': 'thip.media'},
                        'url': 'https://www.thip.media/health-news-fact-check/fact-check-is-radiation-from-apple-airpods-dangerous/30863/',
                        'title': 'Fact Check: Is radiation from Apple AirPods dangerous?',
                        'reviewDate': '2022-05-04T00:00:00Z', 'textualRating': 'Mostly False', 'languageCode': 'en'}]}],
        'nextPageToken': 'CAM'}

for claim in claims_json_data['claims']:
    print(f"text: {claim['text']}")
    for review in claim['claimReview']:
        print(f"url: {review['url']}")
        print(f"title: {review['title']}")
        print(f"textualRating {review['textualRating']}")
        print('\n')
  • Related