Well, this should be so simple. If you keep JSON data in a Javascript variable, then the way you tried should work. See my version of it.

var jsonData = {
    "page" : 1,
    "results" : [{
            "adult" : false,
            "backdrop_path" : "/fLL6WfUXvdQee1fD4xuzNnWfVBk.jpg",
            "genre_ids" : [27, 9648, 80],
            "id" : 176,
            "original_language" : "en",
            "original_title" : "Saw",
            "overview" : "Obsessed with teaching his victims the value of life, a deranged, sadistic serial killer abducts the morally wayward. Once captured, they must face impossible choices in a horrific game of survival. The victims must fight to win their lives back, or die trying...",
            "release_date" : "2004-01-19",
            "poster_path" : "/dHYvIgsax8ZFgkz1OslE4V6Pnf5.jpg",
            "popularity" : 2.897462,
            "title" : "Saw",
            "video" : false,
            "vote_average" : 7.1,
            "vote_count" : 657
        }, {
            "adult" : false,
            "backdrop_path" : "/yKATxJtGY67cXdOmlbWwW6EgPqn.jpg",
            "genre_ids" : [27, 53, 80],
            "id" : 663,
            "original_language" : "en",
            "original_title" : "Saw IV",
            "overview" : "Jigsaw and his apprentice Amanda are dead. Now, upon the news of Detective Kerry's murder, two seasoned FBI profilers, Agent Strahm and Agent Perez, arrive in the terrified community to assist the veteran Detective Hoffman in sifting through Jigsaw's latest grisly remains and piecing together the puzzle. However, when SWAT Commander Rigg is abducted and thrust into a game, the last officer untouched by Jigsaw has but ninety minutes to overcome a series of demented traps and save an old friend...or face the deadly consequences.",
            "release_date" : "2007-10-25",
            "poster_path" : "/veApHw5ARGHWf3ptKf30rOGFY9n.jpg",
            "popularity" : 2.449196,
            "title" : "Saw IV",
            "video" : false,
            "vote_average" : 5.8,
            "vote_count" : 257
        }
    ]
};

console.log(jsonData.results[0].id);
console.log(jsonData.results[1].id);
Answer from Beroza Paul on Stack Overflow
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ get value from jason object in javascript
How to Get Value From JSON Object in JavaScript | Delft Stack
February 2, 2024 - const json = `{ "employee": { "name": "Roy Mustang", "age": 35, "favoriteColor": ["Blue", "Black", "Purple"], "siblings": { "Liza": 20, "Emily": 12 } } }`; var data = JSON.parse(json); var i; for (i in data) { if (data[i] instanceof Object) { console.log(data[i]); } } ... As we have already parsed the data, now letโ€™s extract the individual values for key:value pair with the dot (.) operation.
๐ŸŒ
I'd Rather Be Writing
idratherbewriting.com โ€บ learnapidoc โ€บ docapis_access_json_values.html
Access and print a specific JSON value | I'd Rather Be Writing Blog and API doc course
4 days ago - To pull out the wind speed element from the JSON response and print it to the JavaScript Console, add this to your code sample (which you created in the previous tutorial), right below the console.log(response) line:
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_objects.asp
JSON Object Literals
Each key/value pair is separated by a comma. It is a common mistake to call a JSON object literal "a JSON object". JSON cannot be an object. JSON is a string format. The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-get-a-value-from-a-json-array-in-javascript
How to Get a Value from a JSON Array in JavaScript? - GeeksforGeeks
June 28, 2025 - To retrieve a value from a JSON array in JavaScript, we can use various methods such as accessing the array by index or using built-in methods like find(), map(), etc. In a JSON array, values are stored in an ordered list, which means you can ...
Top answer
1 of 2
3

Well, this should be so simple. If you keep JSON data in a Javascript variable, then the way you tried should work. See my version of it.

var jsonData = {
    "page" : 1,
    "results" : [{
            "adult" : false,
            "backdrop_path" : "/fLL6WfUXvdQee1fD4xuzNnWfVBk.jpg",
            "genre_ids" : [27, 9648, 80],
            "id" : 176,
            "original_language" : "en",
            "original_title" : "Saw",
            "overview" : "Obsessed with teaching his victims the value of life, a deranged, sadistic serial killer abducts the morally wayward. Once captured, they must face impossible choices in a horrific game of survival. The victims must fight to win their lives back, or die trying...",
            "release_date" : "2004-01-19",
            "poster_path" : "/dHYvIgsax8ZFgkz1OslE4V6Pnf5.jpg",
            "popularity" : 2.897462,
            "title" : "Saw",
            "video" : false,
            "vote_average" : 7.1,
            "vote_count" : 657
        }, {
            "adult" : false,
            "backdrop_path" : "/yKATxJtGY67cXdOmlbWwW6EgPqn.jpg",
            "genre_ids" : [27, 53, 80],
            "id" : 663,
            "original_language" : "en",
            "original_title" : "Saw IV",
            "overview" : "Jigsaw and his apprentice Amanda are dead. Now, upon the news of Detective Kerry's murder, two seasoned FBI profilers, Agent Strahm and Agent Perez, arrive in the terrified community to assist the veteran Detective Hoffman in sifting through Jigsaw's latest grisly remains and piecing together the puzzle. However, when SWAT Commander Rigg is abducted and thrust into a game, the last officer untouched by Jigsaw has but ninety minutes to overcome a series of demented traps and save an old friend...or face the deadly consequences.",
            "release_date" : "2007-10-25",
            "poster_path" : "/veApHw5ARGHWf3ptKf30rOGFY9n.jpg",
            "popularity" : 2.449196,
            "title" : "Saw IV",
            "video" : false,
            "vote_average" : 5.8,
            "vote_count" : 257
        }
    ]
};

console.log(jsonData.results[0].id);
console.log(jsonData.results[1].id);
2 of 2
1

It is a better practice to check the datatype of the variable one is about to parse using typeOf json==='object', if it is a valid object then you can do further manipulations or else parse it using json=JSON.parse(json);

๐ŸŒ
TutorialsTonight
tutorialstonight.com โ€บ get-value-from-json-object-in-javascript
Get Value From JSON Object In JavaScript (Example Code)
Above we accessed the value from the JSON object using dot notation but we can also access the value using bracket notation. In the bracket notation, you have to pass the key of the value you want to access as a string.
๐ŸŒ
Thequantizer
thequantizer.com โ€บ the quantizer โ€บ javascript
JavaScript: How to Get JSON value by Key :: The Quantizer
let firstKey = Object.keys(fir... a few ways to do this. One of the most common is a forEach loop. jsonData.forEach((element, i) => { console.log("element: " + i + " is: " + JSON.stringify(element)) });...
๐ŸŒ
ServiceNow Community
servicenow.com โ€บ community โ€บ developer-blog โ€บ fetch-value-from-json โ€บ ba-p โ€บ 2282127
Fetch value from JSON - ServiceNow Community
July 13, 2023 - Save the values into an object then push the object into an array. Use the JSON.stringify() method to convert object/array into string to send it further processing. I will keep updating the content with different scenarios as and when I get more experience in this subject to share also as per your valuable feedback and suggestions.
Find elsewhere
๐ŸŒ
RSWP Themes
rswpthemes.com โ€บ home โ€บ javascript tutorial โ€บ how to get value by key in json object in javascript
How To Get Value By Key In Json Object In Javascript
March 27, 2024 - One straightforward way to access a value in a JSON object is through dot notation. If you know the key name, you can directly access the value associated with it. Hereโ€™s an example: const jsonObject = { key1: 'value1', key2: 'value2' }; const ...
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ javascript-tutorial โ€บ javascript-json-parsing.php
JavaScript JSON Parsing - Tutorial Republic
Let's suppose we've received the following JSON-encoded string from a web server: ... Now, we can simply use the JavaScript JSON.parse() method to convert this JSON string into a JavaScript object and access individual values using the dot notation ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 20799432 โ€บ javascript-retrieve-value-from-json
javascript retrieve value from json - Stack Overflow
JSON objects work as an array. You can access to an element with a key: obj['Account'] // returns dimts obj.Account // works also ยท You should read some tutorial about it, like JSON: What It Is, How It Works, & How to Use It ... var jArray = <?php echo json_encode($_SESSION['return'] ); ?>; var obj = JSON.parse(jArray); var value=obj.Result;
๐ŸŒ
Programmingbasic
programmingbasic.com โ€บ get-value-from-json-object-in-javascript
How to get value from JSON object in JavaScript
March 27, 2025 - Short article on how to get or fetch a value from a JSON object in JavaScript using JSON.parse() method.
Top answer
1 of 6
48

JSON content is basically represented as an associative array in JavaScript. You just need to loop over them to either read the key or the value:

    var JSON_Obj = { "one":1, "two":2, "three":3, "four":4, "five":5 };

    // Read key
    for (var key in JSON_Obj) {
       console.log(key);
       console.log(JSON_Obj[key]);
   }
2 of 6
19

First off, you're not dealing with a "JSON object." You're dealing with a JavaScript object. JSON is a textual notation, but if your example code works ([0].amount), you've already deserialized that notation into a JavaScript object graph. (What you've quoted isn't valid JSON at all; in JSON, the keys must be in double quotes. What you've quoted is a JavaScript object literal, which is a superset of JSON.)

Here, length of this array is 2.

No, it's 3.

So, i need to get the name (like amount or job... totally four name) and also to count how many names are there?

If you're using an environment that has full ECMAScript5 support, you can use Object.keys (spec | MDN) to get the enumerable keys for one of the objects as an array. If not (or if you just want to loop through them rather than getting an array of them), you can use for..in:

var entry;
var name;
entry = array[0];
for (name in entry) {
    // here, `name` will be "amount", "job", "month", then "year" (in no defined order)
}

Full working example:

(function() {
  
  var array = [
    {
      amount: 12185,
      job: "GAPA",
      month: "JANUARY",
      year: "2010"
    },
    {
      amount: 147421,
      job: "GAPA",
      month: "MAY",
      year: "2010"
    },
    {
      amount: 2347,
      job: "GAPA",
      month: "AUGUST",
      year: "2010"
    }
  ];
  
  var entry;
  var name;
  var count;
  
  entry = array[0];
  
  display("Keys for entry 0:");
  count = 0;
  for (name in entry) {
    display(name);
    ++count;
  }
  display("Total enumerable keys: " + count);

  // === Basic utility functions
  
  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }
  
})();

Since you're dealing with raw objects, the above for..in loop is fine (unless someone has committed the sin of mucking about with Object.prototype, but let's assume not). But if the object you want the keys from may also inherit enumerable properties from its prototype, you can restrict the loop to only the object's own keys (and not the keys of its prototype) by adding a hasOwnProperty call in there:

for (name in entry) {
  if (entry.hasOwnProperty(name)) {
    display(name);
    ++count;
  }
}
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ parse
JSON.parse() - JavaScript - MDN Web Docs
The JSON.parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ get-value-for-key-from-nested-json-object-in-javascript
Get value for key from nested JSON object in JavaScript
August 23, 2023 - Step 2: After creating the nested JSON objects, we will define a function called getNestedKeys to access the nested keys of the JSON data. Inside this function we have initiated an if condition, which is checking that the key is directly available ...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ javascript โ€บ how to access json object in javascript
How to access JSON object in JavaScript - Mkyong.com
October 12, 2013 - To access the JSON object in JavaScript, parse it with JSON.parse(), and access it via โ€œ.โ€ or โ€œ[]โ€.
Top answer
1 of 4
154

There are two ways to access properties of objects:

var obj = {a: 'foo', b: 'bar'};

obj.a //foo
obj['b'] //bar

Or, if you need to dynamically do it:

var key = 'b';
obj[key] //bar

If you don't already have it as an object, you'll need to convert it.

For a more complex example, let's assume you have an array of objects that represent users:

var users = [{name: 'Corbin', age: 20, favoriteFoods: ['ice cream', 'pizza']},
             {name: 'John', age: 25, favoriteFoods: ['ice cream', 'skittle']}];

To access the age property of the second user, you would use users[1].age. To access the second "favoriteFood" of the first user, you'd use users[0].favoriteFoods[2].

Another example: obj[2].key[3]["some key"]

That would access the 3rd element of an array named 2. Then, it would access 'key' in that array, go to the third element of that, and then access the property name some key.


As Amadan noted, it might be worth also discussing how to loop over different structures.

To loop over an array, you can use a simple for loop:

var arr = ['a', 'b', 'c'],
    i;
for (i = 0; i < arr.length; ++i) {
    console.log(arr[i]);
}

To loop over an object is a bit more complicated. In the case that you're absolutely positive that the object is a plain object, you can use a plain for (x in obj) { } loop, but it's a lot safer to add in a hasOwnProperty check. This is necessary in situations where you cannot verify that the object does not have inherited properties. (It also future proofs the code a bit.)

var user = {name: 'Corbin', age: 20, location: 'USA'},
    key;

for (key in user) {
    if (user.hasOwnProperty(key)) {
        console.log(key + " = " + user[key]);
    }
}    

(Note that I've assumed whatever JS implementation you're using has console.log. If not, you could use alert or some kind of DOM manipulation instead.)

2 of 4
21

Try the JSON Parser by Douglas Crockford at github. You can then simply create a JSON object out of your String variable as shown below:

var JSONText = '{"c":{"a":[{"name":"cable - black","value":2},{"name":"case","value":2}]},"o":{"v":[{"name":"over the ear headphones - white/purple","value":1}]},"l":{"e":[{"name":"lens cleaner","value":1}]},"h":{"d":[{"name":"hdmi cable","value":1},{"name":"hdtv essentials (hdtv cable setup)","value":1},{"name":"hd dvd \u0026 blue-ray disc lens cleaner","value":1}]}'

var JSONObject = JSON.parse(JSONText);
var c = JSONObject["c"];
var o = JSONObject["o"];
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_parse.asp
JSON.parse()
A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse(), and the data becomes a JavaScript object.