I have this JSON file
{
"document_tone": {
"tone_categories": [
{
"category_id": "emotion_tone",
"category_name": "Emotion Tone",
"tones": [
{
"tone_id": "anger",
"tone_name": "Anger",
"score": 0.380012
},
{
"tone_id": "disgust",
"tone_name": "Disgust",
"score": 0.093832
},
{
"tone_id": "fear",
"tone_name": "Fear",
"score": 0.156902
},
{
"tone_id": "joy",
"tone_name": "Joy",
"score": 0.204329
},
{
"tone_id": "sadness",
"tone_name": "Sadness",
"score": 0.243196
}
]
},
{
"category_id": "language_tone",
"category_name": "Language Tone",
"tones": [
{
"tone_id": "analytical",
"tone_name": "Analytical",
"score": 0.978091
},
{
"tone_id": "confident",
"tone_name": "Confident",
"score": 0.0
},
{
"tone_id": "tentative",
"tone_name": "Tentative",
"score": 0.0
}
]
},
{
"category_id": "social_tone",
"category_name": "Social Tone",
"tones": [
{
"tone_id": "openness_big5",
"tone_name": "Openness",
"score": 0.453176
},
{
"tone_id": "conscientiousness_big5",
"tone_name": "Conscientiousness",
"score": 0.707102
},
{
"tone_id": "extraversion_big5",
"tone_name": "Extraversion",
"score": 0.856905
},
{
"tone_id": "agreeableness_big5",
"tone_name": "Agreeableness",
"score": 0.793529
},
{
"tone_id": "emotional_range_big5",
"tone_name": "Emotional Range",
"score": 0.835602
}
]
}
]
},
"sentences_tone": [
{
"sentence_id": 0,
"input_from": 0,
"input_to": 48,
"text": "SVG transform on text attribute works excellent!",
"tone_categories": [
{
"category_id": "emotion_tone",
"category_name": "Emotion Tone",
"tones": [
{
"tone_id": "anger",
"tone_name": "Anger",
"score": 0.262452
},
{
"tone_id": "disgust",
"tone_name": "Disgust",
"score": 0.031795
},
{
"tone_id": "fear",
"tone_name": "Fear",
"score": 0.104458
},
{
"tone_id": "joy",
"tone_name": "Joy",
"score": 0.555167
},
{
"tone_id": "sadness",
"tone_name": "Sadness",
"score": 0.103881
}
]
},
{
"category_id": "language_tone",
"category_name": "Language Tone",
"tones": [
{
"tone_id": "analytical",
"tone_name": "Analytical",
"score": 0.801827
},
{
"tone_id": "confident",
"tone_name": "Confident",
"score": 0.0
},
{
"tone_id": "tentative",
"tone_name": "Tentative",
"score": 0.0
}
]
},
{
"category_id": "social_tone",
"category_name": "Social Tone",
"tones": [
{
"tone_id": "openness_big5",
"tone_name": "Openness",
"score": 0.285863
},
{
"tone_id": "conscientiousness_big5",
"tone_name": "Conscientiousness",
"score": 0.441621
},
{
"tone_id": "extraversion_big5",
"tone_name": "Extraversion",
"score": 0.710666
},
{
"tone_id": "agreeableness_big5",
"tone_name": "Agreeableness",
"score": 0.521599
},
{
"tone_id": "emotional_range_big5",
"tone_name": "Emotional Range",
"score": 0.35773
}
]
}
]
},
{
"sentence_id": 1,
"input_from": 49,
"input_to": 114,
"text": "This snippet for example will increase your text by 2x at Y-axis.",
"tone_categories": [
{
"category_id": "emotion_tone",
"category_name": "Emotion Tone",
"tones": [
{
"tone_id": "anger",
"tone_name": "Anger",
"score": 0.278447
},
{
"tone_id": "disgust",
"tone_name": "Disgust",
"score": 0.137868
},
{
"tone_id": "fear",
"tone_name": "Fear",
"score": 0.177609
},
{
"tone_id": "joy",
"tone_name": "Joy",
"score": 0.052633
},
{
"tone_id": "sadness",
"tone_name": "Sadness",
"score": 0.426658
}
]
},
{
"category_id": "language_tone",
"category_name": "Language Tone",
"tones": [
{
"tone_id": "analytical",
"tone_name": "Analytical",
"score": 0.932977
},
{
"tone_id": "confident",
"tone_name": "Confident",
"score": 0.0
},
{
"tone_id": "tentative",
"tone_name": "Tentative",
"score": 0.0
}
]
},
{
"category_id": "social_tone",
"category_name": "Social Tone",
"tones": [
{
"tone_id": "openness_big5",
"tone_name": "Openness",
"score": 0.609711
},
{
"tone_id": "conscientiousness_big5",
"tone_name": "Conscientiousness",
"score": 0.872715
},
{
"tone_id": "extraversion_big5",
"tone_name": "Extraversion",
"score": 0.881295
},
{
"tone_id": "agreeableness_big5",
"tone_name": "Agreeableness",
"score": 0.881589
},
{
"tone_id": "emotional_range_big5",
"tone_name": "Emotional Range",
"score": 0.938061
}
]
}
]
}
]
}
I want to extract the "tones" in "document_tone" -> "tone_categories", meaning I want to extract the scores of anger, disgust, fear, joy and sadness and print them out. The first ones shown in the file.
The output should be this simple:
0.380012, 0.093832, 0.156902, 0.204329, 0.243196
I've made this code:
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader("myfile.json"));
JSONObject document_tone = (JSONObject) jsonObject.get("document_tone");
JSONObject tone_categories = (JSONObject) document_tone.get("tone_categories");
JSONObject tones = (JSONObject) tone_categories.get("tones");
double joy = (double) tones.get("joy");
double sadness = (double) tones.get("sadness");
double disgust = (double) tones.get("disgust");
double anger = (double) tones.get("anger");
double fear = (double) tones.get("fear");
System.out.println(joy + " ;" + sadness + " ;" + disgust + " ;" + anger + " ;" + fear);
But had no success. I suspect there is something wrong in getting the nested objects one after the other.