Saturday 22 April 2017

How to parse a JSON.Stringfy?

Allow me to preface this by saying that I did look at both .parse and how to make it work with .stringify, but nothing worked.

In my code, based on what IBM has for its natural language processing (https://www.ibm.com/watson/developercloud/natural-language-understanding/api/v1/?node#post-analyze) I have the following part:

var NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js');

var natural_language_understanding = new NaturalLanguageUnderstandingV1({
  'username': 'f3550111-bd50-4e5f-8d44-8f0cc493c05e',
  'password': 'XSG0rEW0UOlB',
  'version_date': '2017-02-27'
});


var parameters = {
  'text': 'A most interesting and amusing text indeed, about as amusing as IBMs godawful documentations',

  'features': {
    'entities': {
      'emotion': true,
      'sentiment': true,
      'limit': 2
    },
    'keywords': {
      'emotion': true,
      'sentiment': true,
      'limit': 2
    }
  }
}

I stringify the response per the example code as follows:

function calculateSentiment() {

    natural_language_understanding.analyze(parameters, function(err, data) {
        if (err)
            console.log('error:', err);
        else {
            var myData = JSON.stringify(data, null, 2);
            //console.log(myData[0][0][0]);
            //var json = JSON.stringify(JSON.parse(data));
            //console.log("Score: " + json);

            //var prsed = (JSON.parse(data));
            console.log("Parsed: " + myData);
            }
        }
    ); 
}

As you can see, I have all those comments which were my attempts at parsing. If I just have it print the data all I get is [object Object]. How can I properly parse this data? The corresponding result of the stringify is (the "Parsed: " part is from my own console output):

Parsed: {
  "keywords": [
    {
      "text": "amusing text",
      "sentiment": {
        "score": 0.768903
      },
      "relevance": 0.98388,
      "emotion": {
        "sadness": 0.260734,
        "joy": 0.407077,
        "fear": 0.113361,
        "disgust": 0.006258,
        "anger": 0.028386
      }
    },
    {
      "text": "IBMs godawful documentation",
      "sentiment": {
        "score": 0
      },
      "relevance": 0.809662,
      "emotion": {
        "sadness": 0.156867,
        "joy": 0.412954,
        "fear": 0.094685,
        "disgust": 0.082537,
        "anger": 0.101907
      }
    }
  ],
  "entities": [],
  "language": "en"
}
Parsed: {
  "keywords": [
    {
      "text": "amusing text",
      "sentiment": {
        "score": 0.768903
      },
      "relevance": 0.98388,
      "emotion": {
        "sadness": 0.260734,
        "joy": 0.407077,
        "fear": 0.113361,
        "disgust": 0.006258,
        "anger": 0.028386
      }
    },
    {
      "text": "IBMs godawful documentation",
      "sentiment": {
        "score": 0
      },
      "relevance": 0.809662,
      "emotion": {
        "sadness": 0.156867,
        "joy": 0.412954,
        "fear": 0.094685,
        "disgust": 0.082537,
        "anger": 0.101907
      }
    }
  ],
  "entities": [],
  "language": "en"
}
Parsed: {
  "keywords": [
    {
      "text": "amusing text",
      "sentiment": {
        "score": 0.768903
      },
      "relevance": 0.98388,
      "emotion": {
        "sadness": 0.260734,
        "joy": 0.407077,
        "fear": 0.113361,
        "disgust": 0.006258,
        "anger": 0.028386
      }
    },
    {
      "text": "IBMs godawful documentation",
      "sentiment": {
        "score": 0
      },
      "relevance": 0.809662,
      "emotion": {
        "sadness": 0.156867,
        "joy": 0.412954,
        "fear": 0.094685,
        "disgust": 0.082537,
        "anger": 0.101907
      }
    }
  ],
  "entities": [],
  "language": "en"
}



via SomeStudent

No comments:

Post a Comment