I am receiving some JSON from IBM Watson's tone analyzer API in the format shown below for a piece of text. What I would like to do is capture JSON in an object with a property and value for the main tones, eg. anger:.218 disgust:2.20 etc. for each section of text analyzed. As I analyze more pieces of text I want to add them to an array of these objects.
At this stage, I just want to do something with the api using SwiftyJSON but am confused by the syntax:
The following code, just prints out as nil:
let anger = JSON(value)["results"][0]["anger"].array?.map { json in
json["anger"].stringValue
}
print (anger)
Would appreciate guidance on how to actually grab the tones and their values.... Thanks in advance for any suggestions.
JSON looks like this:
{
"document_tone" = {
"tone_categories" = (
{
"category_id" = "emotion_tone";
"category_name" = "Emotion Tone";
tones = (
{
score = "0.218727";
"tone_id" = anger;
"tone_name" = Anger;
},
{
score = "0.210102";
"tone_id" = disgust;
"tone_name" = Disgust;
},
{
score = "0.060026";
"tone_id" = fear;
"tone_name" = Fear;
},
{
score = "0.076444";
"tone_id" = joy;
"tone_name" = Joy;
},
{
score = "0.176849";
"tone_id" = sadness;
"tone_name" = Sadness;
}
);
},
{
"category_id" = "language_tone";
"category_name" = "Language Tone";
tones = (
{
score = 0;
"tone_id" = analytical;
"tone_name" = Analytical;
},
{
score = 0;
"tone_id" = confident;
"tone_name" = Confident;
},
{
score = 0;
"tone_id" = tentative;
"tone_name" = Tentative;
}
);
},
{
"category_id" = "social_tone";
"category_name" = "Social Tone";
tones = (
{
score = "0.02278";
"tone_id" = "openness_big5";
"tone_name" = Openness;
},
{
score = "0.340597";
"tone_id" = "conscientiousness_big5";
"tone_name" = Conscientiousness;
},
{
score = "0.541852";
"tone_id" = "extraversion_big5";
"tone_name" = Extraversion;
},
{
score = "0.545246";
"tone_id" = "agreeableness_big5";
"tone_name" = Agreeableness;
},
{
score = "0.743194";
"tone_id" = "emotional_range_big5";
"tone_name" = "Emotional Range";
}
);
}
);
};
}
You can quickly and easily accomplish this with
Codable
structs in swift 4 andURLSession
.Here is a pure swift solution to your problem. The struct properties map to your json tree, and you might need to change the structs to match the json you're receiving. The json you posted above is in an invalid format otherwise I could have mapped it correctly.