56

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
Syscall
  • 18,131
  • 10
  • 32
  • 49
Shojib Flamon
  • 857
  • 3
  • 10
  • 18

10 Answers10

78

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']); 
jdphenix
  • 14,226
  • 3
  • 39
  • 70
33

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

console.log("%j", jsonObj);
Naren Chejara
  • 868
  • 9
  • 7
28

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

success:function(data){
     console.log(JSON.stringify(data));
}
Sanjeev Singh
  • 3,810
  • 2
  • 30
  • 38
Ziga Petek
  • 3,745
  • 4
  • 29
  • 39
24
{"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
Akinjide
  • 2,663
  • 20
  • 28
9

I usually do like this:

console.log(JSON.stringify(data, undefined, 4));
liujigang
  • 301
  • 3
  • 5
7

If you just want to print object then

console.log(JSON.stringify(data)); //this will convert json to string;

If you want to access value of field in object then use

console.log(data.input_data);
Naqeeb Sial
  • 647
  • 6
  • 11
2

You can also use util library:

const util = require("util")

> myObject = {1:2, 3:{5:{6:{7:8}}}}
{ '1': 2, '3': { '5': { '6': [Object] } } }

> util.inspect(myObject, {showHidden: true, depth: null})
"{\n  '1': 2,\n  '3': { '5': { '6': { '7': 8 } } }\n}"

> JSON.stringify(myObject)
'{"1":2,"3":{"5":{"6":{"7":8}}}}'

original source : https://stackoverflow.com/a/10729284/8556340

Roeniss
  • 186
  • 2
  • 10
1

This is an old post but I'm chiming in because (as Narem briefly mentioned) a few of the printf-like features are available with the console.log formatters. In the case of the question, you can benefit from the string, number or json formatters for your data.

Examples:

console.log("Quantity %s, Price: %d", data.quantity-row_122, data.price-row_122);
console.log("Quantity and Price Data %j", data);
Corndog
  • 51
  • 2
0

Linked to the tittle How to print JSON data in console.log?, possibly useful notes not too far from the subject, and a tiny useful tool:

Whenever we have to work on a JSON file, via the console, we can display the url, or drop, a JSON file in a tab.


In Firefox, we have a nice JSONview, but still, we can't use the object from the console, at least directly. To access the JSON object, we have to dive a little:

JSON.parse(window.JSONView.json.data)

In Chromium, simply, a JSON is displayed as text.

JSON.parse(document.body.textContent)

From there, here is a simple Bookmarklet for both browsers.

javascript:void (async()=>{let e=window.JSONView?JSON.parse(window.JSONView.json.data):JSON.parse(document.body.textContent);console.log(e)})();

enter image description here


Tldr: In a browser tab, drop a JSON file. After clicking this bookmarklet, the JSON object get displayed in the console, and from there we can use it as global variable, in the best place to do so: the browser console!

NVRM
  • 8,789
  • 1
  • 69
  • 80
-1

Object

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