JT28
JT28

Reputation: 479

Extract value from json and filter it using python

I'm very new to python.I'm learning it.

I have json file which contains the following json data.

{
"document_tone": {
"tone_categories": [
  {
    "category_id": "emotion_tone", 
    "tones": [
      {
        "tone_name": "Anger", 
        "score": 0.041202, 
        "tone_id": "anger"
      }, 
      {
        "tone_name": "Disgust", 
        "score": 0.054272, 
        "tone_id": "disgust"
      }, 
      {
        "tone_name": "Fear", 
        "score": 0.080706, 
        "tone_id": "fear"
      }, 
      {
        "tone_name": "Joy", 
        "score": 0.813125, 
        "tone_id": "joy"
      }, 
      {
        "tone_name": "Sadness", 
        "score": 0.155878, 
        "tone_id": "sadness"
      }
    ], 
    "category_name": "Emotion Tone"
  }, 
  {
    "category_id": "writing_tone", 
    "tones": [
      {
        "tone_name": "Analytical", 
        "score": 0.0, 
        "tone_id": "analytical"
      }, 
      {
        "tone_name": "Confident", 
        "score": 0.0, 
        "tone_id": "confident"
      }, 
      {
        "tone_name": "Tentative", 
        "score": 0.0, 
        "tone_id": "tentative"
      }
    ], 
    "category_name": "Writing Tone"
  }, 
  {
    "category_id": "social_tone", 
    "tones": [
      {
        "tone_name": "Openness", 
        "score": 0.028, 
        "tone_id": "openness_big5"
      }, 
      {
        "tone_name": "Conscientiousness", 
        "score": 0.314, 
        "tone_id": "conscientiousness_big5"
      }, 
      {
        "tone_name": "Extraversion", 
        "score": 0.944, 
        "tone_id": "extraversion_big5"
      }, 
      {
        "tone_name": "Agreeableness", 
        "score": 0.982, 
        "tone_id": "agreeableness_big5"
      }, 
      {
        "tone_name": "Emotional Range", 
        "score": 0.865, 
        "tone_id": "neuroticism_big5"
      }
    ], 
    "category_name": "Social Tone"
  }
]
}
}

First I want to extract following fields and after extracting the fields I need "tone_name" with highest score.

 "tones": [
      {
        "tone_name": "Anger", 
        "score": 0.041202, 
        "tone_id": "anger"
      }, 
      {
        "tone_name": "Disgust", 
        "score": 0.054272, 
        "tone_id": "disgust"
      }, 
      {
        "tone_name": "Fear", 
        "score": 0.080706, 
        "tone_id": "fear"
      }, 
      {
        "tone_name": "Joy", 
        "score": 0.813125, 
        "tone_id": "joy"
      }, 
      {
        "tone_name": "Sadness", 
        "score": 0.155878, 
        "tone_id": "sadness"
      }
    ]

My Output should be like : joy

My Code is

import json
with open('data.json','r') as f:
for line in f:
    line = line.strip()
    print line
    parsedJson = json.loads(line)
    print parsedJson
    for tone in parsedJson['document_tone']['tone_categories'][0]['tones']:
        print(tone['tone_name'])

My data.json file contains

"{\n  \"document_tone\": {\n    \"tone_categories\": [\n      {\n            
 \"category_id\": \"emotion_tone\", \n        \"tones\": [\n                        
 {\n            \"tone_name\": \"Anger\", \n            \"score\": 
 0.372974, \n            \"tone_id\": \"anger\"\n          }, \n          
 {\n            \"tone_name\": \"Disgust\", \n            \"score\": 
 0.114389, \n            \"tone_id\": \"disgust\"\n          }, \n          
 {\n            \"tone_name\": \"Fear\", \n            \"score\": 
 0.083108, \n            \"tone_id\": \"fear\"\n          }, \n          
 {\n            \"tone_name\": \"Joy\", \n            \"score\": 
 0.028716, \n            \"tone_id\": \"joy\"\n          }, \n          
 {\n            \"tone_name\": \"Sadness\", \n            \"score\": 
 0.461562, \n            \"tone_id\": \"sadness\"\n          }\n        
 ], \n        \"category_name\": \"Emotion Tone\"\n      }, \n      
 {\n        \"category_id\": \"writing_tone\", \n        \"tones\": 
 [\n          {\n            \"tone_name\": \"Analytical\", \n            
 \"score\": 0.722, \n            \"tone_id\": \"analytical\"\n          
 }, \n          {\n            \"tone_name\": \"Confident\", \n            
 \"score\": 0.0, \n            \"tone_id\": \"confident\"\n          
 }, \n          {\n            \"tone_name\": \"Tentative\", \n              
 \"score\": 0.0, \n            \"tone_id\": \"tentative\"\n          
 }\n        ], \n        \"category_name\": \"Writing Tone\"\n      }, 
 \n      {\n        \"category_id\": \"social_tone\", \n        
 \"tones\": [\n          {\n            \"tone_name\": \"Openness\", 
 \n            \"score\": 0.015, \n            \"tone_id\": 
 \"openness_big5\"\n          }, \n          {\n            
 \"tone_name\": \"Conscientiousness\", \n            \"score\": 0.045, 
 \n            \"tone_id\": \"conscientiousness_big5\"\n          }, 
 \n          {\n            \"tone_name\": \"Extraversion\", \n            
 \"score\": 0.722, \n            \"tone_id\": \"extraversion_big5\"\n          
 }, \n          {\n            \"tone_name\": \"Agreeableness\", \n            
 \"score\": 0.706, \n            \"tone_id\": \"agreeableness_big5\"\n          
 }, \n          {\n            \"tone_name\": \"Emotional Range\", \n               
 \"score\": 0.974, \n            \"tone_id\": \"neuroticism_big5\"\n          
 }\n        ], \n        \"category_name\": \"Social Tone\"\n      }\n    
 ]\n  }\n}"

Upvotes: 0

Views: 682

Answers (1)

Keatinge
Keatinge

Reputation: 4341

Basically to do what you want here you just have to navigate your way through the dict till you get your list of tones, then loop through each tone, and for that tone print out its tone_name

parsedJson = json.loads(jsonFile)
for tone in parsedJson['document_tone']['tone_categories'][0]['tones']:
    print(tone['tone_name'])
# Anger
# Disgust
# Fear
# Joy
# Sadness

Here's your fixed code

import json

jsonText = None
with open('data.json','r') as f:
    jsonText = f.read()

parsedJson = json.loads(jsonText)

for tone in parsedJson['document_tone']['tone_categories'][0]['tones']:
    print(tone['tone_name'])

Upvotes: 1

Related Questions