3

I'm having trouble getting a specific value from a JSON object.

My program queries wolframalpha which returns an object "result" using the following code:

var wolfram = require('wolfram').createClient("[CENSORED]")

wolfram.query("integrate 2x", function(err, result) {
  if(err) throw err
  console.log("Result: %j", result)
})

It returns the following JSON:

[
  {
    "subpods": 
      [{
      "title":"",
      "value":" integral 2 x dx = x^2+constant",
      "image":"http://www5a.wolframalpha.com/Calculate/MSP/MSP36002050fgg595dgib5a000031a456025754352g?MSPStoreType=image/gif&s=59"
      }],
    "primary":true
  },
  {
      "subpods": [{
      "title":"",
      "value":"",
      "image":"http://www5a.wolframalpha.com/Calculate/MSP/MSP36012050fgg595dgib5a000055e24iecig9cc4ga?MSPStoreType=image/gif&s=59"
    }],
    "primary":false
  }
]

I'm trying to get "value" from the first subpod. I tried: var newResults = result.subpods[0].value;

but this gave me an error: TypeError: Cannot read property '0' of undefined

I've been trying different combinations for at least the last hour. Please help!

Thank you for your time,

Bobbyg

bobbyg603
  • 3,536
  • 2
  • 19
  • 30
  • 1
    You need to parse the json to turn it into an object: http://stackoverflow.com/questions/5726729/how-to-parse-json-using-nodejs – Jack Nov 14 '13 at 17:20

2 Answers2

9

result looks like an array.

Try:

var newResults = result[0].subpods[0].value;
tymeJV
  • 103,943
  • 14
  • 161
  • 157
4

Try the following code..

var newResults = result[0].subpods[0].value;
Hasib Tarafder
  • 5,773
  • 3
  • 30
  • 44