How to print JSON data in console.log?

I cant access JSON data from javascript. Please help me how to access data from JSON data in javascript.

i have a JSON data like

{"success":true,"input_data":{"quantity-row_122":"1","price-row_122":" 35.1 "}}

i have tried console.log(data) but log print object object

success:function(data){
     console.log(data);
}

How to print console.log particular data? I need to print

quantity-row_122 = 1
price-row_122 = 35.1

Solution 1:

console.log(JSON.stringify(data)) will do what you need. I'm assuming that you're using jQuery based on your code.

If you're wanting those two particular values, you can just access those and pass them to log.

console.log(data.input_data['quantity-row_122']); 
console.log(data.input_data['price-row_122']); 

Solution 2:

I used '%j' option in console.log to print JSON objects

console.log("%j", jsonObj);

Solution 3:

To output an object to the console, you have to stringify the object first:

success:function(data){
     console.log(JSON.stringify(data));
}

Solution 4:

{"success":true,"input_data":{"quantity-row_122":"1","price-row_122":" 35.1 "}}

console.dir() will do what you need. It will give you a hierarchical structure of the data.

success:function(data){
     console.dir(data);
}

like so

> Object
  > input_data: Object
      price-row_122: " 35.1 "
      quantity-row_122: "1"
    success: true

I don't think you need console.log(JSON.stringify(data)).

To get the data you can do this without stringify:

console.log(data.success); // true
console.log(data.input_data['quantity-row_122']) // "1"
console.log(data.input_data['price-row_122']) // " 35.1 "

Note

The value from input_data Object will be typeof "1": String, but you can convert to number(Int or Float) using ParseInt or ParseFloat, like so:

 typeof parseFloat(data.input_data['price-row_122'], 10) // "number"
 parseFloat(data.input_data['price-row_122'], 10) // 35.1