0

I have below Json output and i am trying to read sample_rate field. Before reading i am checking if any data exists

{
  "responseHeader": {
    "status": 0,
    "QTime": 0
  },
  "response": {
    "numFound": 1,
    "start": 0,
    "docs": [
      {
        "sample_rate": 5000
      }
    ]
  }
}

I am trying to do this using below code but getting error TypeError: Cannot read property 'hasOwnProperty' of undefined

function(error, response, body){
                if(!error && response.statusCode == 200) {
                    console.log("inside t1");
                    let tsales = JSON.parse(body);
                    var tsales2 = JSON.stringify(tsales);
                     console.log(tsales2);
                    console.log("inside t2");
                    if (tsales2.response.hasOwnProperty("docs")) {
                        console.log("inside t3");
                        let reply = `${responseText} ${tsales2.response["docs"][0]["sample_rate"]}`;
                        sendTextMessage(sender, reply);
                    } else {
                        let tsales = JSON.parse(body);
                        console.log("inside t4");
                        sendTextMessage(sender,
                            `No sales available for ${parameters["brand"]}`);
                    }
Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
Raj
  • 47
  • 2
  • 8
  • Replace `if (tsales2.response.hasOwnProperty("docs")) {` with `if (tsales2.response.docs) {` – Maria Ines Parnisari Apr 23 '17 at 23:28
  • 1
    `JSON.stringify` converts the data to a string instead of an object. So if you, in fact, have an object with your `JSON.parse`, you should just work with that - `tsales.response.hasOwnProperty("docs");` – Ken H. Apr 23 '17 at 23:29
  • using JSON.stringify is good for your `console.log` output - but use the object for accessing values within your code. – Ken H. Apr 23 '17 at 23:32
  • And you should not have to JSON.parse again in that last conditional block. And use that same variable for `let reply = \`${responseText} ${tsales.response["docs"][0]["sample_rate"]}\`;` – Ken H. Apr 23 '17 at 23:33

3 Answers3

0

Just use

if (tsales.response.docs)

OR

if (tsales.response.hasOwnProperty("docs"))

Referring to Difference between JSON.stringify and JSON.parse
JSON.parse() turns a string of JSON text into a Javascript object which you can use to reference your hasOwnProperty method.

Community
  • 1
  • 1
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
0

Some observations :

  • Why JSON.parse(body) and then JSON.stringify(tsales) ?. If you want to parse the JSON Object then no need to do JSON.stringify(tsales).
  • JSON.stringify(tsales) will convert the JSON Object into JSON String then this statement tsales2.response.hasOwnProperty("docs") will return false as tsales2 is no more a JSON Object.
  • TypeError: Cannot read property 'hasOwnProperty' of undefined

    means that tsales2.response is not exist or we can say not defined as you converted it into JSON String.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

JSON.stringify(tsales) will convert the tsales JSON object into JSON string object, And that will not have the hasOwnProperty method in string object that's why it's return false and execute the else block, To Solve the issue no need to stringify the tsales like below code

function(error, response, body){
  if(!error && response.statusCode == 200) {
    console.log("inside t1");
    let tsales = JSON.parse(body);
    console.log(tsales);
    console.log("inside t2");
    if (tsales.response.hasOwnProperty("docs")) {
      console.log("inside t3");
      let reply = `${responseText} ${tsales.response["docs"][0]["sample_rate"]}`;
      sendTextMessage(sender, reply);
    } else {
      let tsales = JSON.parse(body);
      console.log("inside t4");
      sendTextMessage(sender,
        `No sales available for ${parameters["brand"]}`);
    }
  }
}
mohan rathour
  • 420
  • 2
  • 12