1

I am using Perspective API (you can check out at: http://perspectiveapi.com/) for my discord application. I am sending an analyze request and api returning this:

{
  "attributeScores": {
    "TOXICITY": {
      "spanScores": [
        {
          "begin": 0,
          "end": 22,
          "score": {
            "value": 0.9345592,
            "type": "PROBABILITY"
          }
        }
      ],
      "summaryScore": {
        "value": 0.9345592,
        "type": "PROBABILITY"
      }
    }
  },
  "languages": [
    "en"
  ],
  "detectedLanguages": [
    "en"
  ]
}

I need to get "value" in "summaryScore" as an integer. I searched it on Google, but i just found reading value for not categorized or only 1 times categorized json files. How can i do that?

Note: Sorry if i asked something really easy or if i slaughtered english. My primary language is not english and i am not much experienced on node.js

Hypenord
  • 25
  • 5

1 Answers1

0

First you must make sure the object you have recived is presived by nodeJS as a JSON object, look at this answer for how first. After the object is stored as a JSON object you can do the following:

Reading from nested objects or arrays is as easy as doing this:

object.attributeScores.TOXICITY.summaryScore.value

If you look closer to the object and its structure you can see that the root object (the first {}) contains 3 values: "attributeScores", "languages" and "detectedLanguages".

The field you are looking for exists inside the "summeryScore" object that exists inside the "TOXICITY" object and so on. Thus you need to traverse the object structure until you get to the value you need.

Cabbe
  • 46
  • 1
  • 3
  • 1
    I already know summaryScore is in TOXICITY and TOXICITY in attributeScores. I just not parsed value. Thank you so much :D – Hypenord Apr 26 '21 at 08:33