Whether you choose the first or the third option depends on your use case. If you are modeling many different instances of the same type of thing, choose the first. For example, you have a list of people. If you are modeling many different attributes of one thing, choose the third. You can have repeated keys in the first format, but not in the third.

The second option is terrible, and I've yet to find an appropriate use case for it. The reason it's terrible, in addition to being more verbose, is that for single-level JSON, it breaks most libraries' automatic conversion to a dictionary/map. For deeply-nested JSON, it breaks the XPath-like query interface.

This makes it a pain to work with. And if you don't know your keys at compile time, you will want a dictionary or XPath interface, because you won't be able to convert it to a class. It may not seem like a big deal now, but the longer you have a data format, the harder it will be to change.

Answer from Karl Bielefeldt on Stack Exchange
🌐
Micro Focus
microfocus.com › documentation › silk-performer › 195 › en › silkperformer-195-webhelp-en › GUID-6AFC32B4-6D73-4FBA-AD36-E42261E2D77E.html
JSON Object Structure - Micro Focus
A key-value pair consists of a key and a value, separated by a colon (:). The key is a string, which identifies the key-value pair. The value can be any of the following data types: { } //Empty JSON object { “StringProperty”: “StringValue”, “NumberProperty”: 10, “FloatProperty”: ...
🌐
Squarespace
developers.squarespace.com › what-is-json
JSON - Developer Portal
Key/Value Pair: A key value pair follows a specific syntax, with the key followed by a colon followed by the value. Key/value pairs are comma separated. Let's take one line from the JSON sample above and identify each part of the code.
🌐
DigitalOcean
digitalocean.com › community › tutorials › an-introduction-to-json
An Introduction to JSON | DigitalOcean
August 24, 2022 - Most data used in JSON ends up being encapsulated in a JSON object. Key-value pairs have a colon between them as in "key" : "value". Each key-value pair is separated by a comma, so the middle of a JSON lists as follows: "key" : "value", "key" : "value", "key": "value".
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;
  }
}
Find elsewhere
🌐
DEV Community
dev.to › msnmongare › understanding-json-keys-values-and-data-structures-4k67
Understanding JSON: Keys, Values, and Data Structures - DEV Community
May 29, 2025 - Values store the data (multiple types supported). Structure matters—consistent key naming prevents errors. Now that you know the basics, try parsing a JSON API response or creating your own configuration file!
🌐
RestfulAPI
restfulapi.net › home › json › valid key names in json
Valid Key Names in JSON - REST API Tutorial
November 5, 2023 - The keys are written in double quotes and values are written according to their data types. Note that the keys are case-sensitive so "id" and "ID" are treated as two different key names.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How to add a key/value pair to an existing json file?
September 8, 2023 - The Json file would look some like this: { “data”: [ { "id": "1700148573403304137", "text": "Hi ! " }, { "id": "1700147255322259501", "text": "Hello" } ] For the final I want to add something like: “te…
🌐
Thequantizer
thequantizer.com › the quantizer › javascript
JavaScript: How to Get JSON value by Key :: The Quantizer
so finally to get the value of price we can do the following · let firstKey = Object.keys(firstObj)[0]; let firstKeyValue = firstObj[firstKey]; will return · 13300000 · If we wanted preform operations on all the elements in the array we have 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)) }); jsonData = JS object ·
🌐
W3Schools
w3schools.com › js › js_json_syntax.asp
JSON Syntax
A name/value pair consists of a ... The JSON format is almost identical to JavaScript objects. In JSON, keys must be strings, written with double quotes:...
🌐
Postman
community.postman.com › help hub
How to transform JSON body to key=value&key2=value2 - Help Hub - Postman Community
February 20, 2024 - Hi All, I’m trying to transform the JSON body of the request into a string formatted as 'key1=value1&key2=value2&key3=value3'. I have a regular JavaScript line that works great: const bodySignParams = Object.entries(params).map(([key, val]) => key + '=' + val).join('&'); where ‘params’ is a JSON object If I use it in an online Javascript sandbox, the result is perfect.
🌐
Medium
medium.com › @lowenthal_jason › key-value-pairs-bafb734cf33c
Key/ Value Pairs. At the root of all JSON you’ll find… | by Jason Lowenthal | Medium
September 1, 2016 - At the root of all JSON you’ll find structures of Key/Value pairs. There’s more than one valid way to represent data this way, but I’m of the quite strong stance that one way is much stronger as an API data set than the other.
🌐
Online Tools
onlinetools.com › json › extract-json-keys
Extract JSON Keys – Online JSON Tools
... Depth of the object(s) from which you want to extract keys. To extract all keys, enter "*". To extract keys from specific depths, use depth values like "1, 2, 4" or ranges like "2-5".
🌐
DEV Community
dev.to › tooleroid › master-json-key-value-pairs-a-complete-guide-59b
Master JSON Key-Value Pairs: A Complete Guide - DEV Community
December 23, 2024 - Think of key-value pairs as the building blocks of JSON—like a digital address book where each name (key) points to a specific phone number (value). In JSON, these pairs are the heart and soul of how data gets organized and accessed.
🌐
Safe Community
community.safe.com › home › forums › fme form › general › how to parse out json data -- getting key value pairs to into separate attribute fields.
How to Parse out Json data -- getting Key Value pairs to into separate attribute fields. | Community
June 19, 2023 - The JSONFragmenter is flattenting the content after it has been processed and is found in the _response_body attribute. The JSON Query that I created in my previous post: json["Comments"][*]["Comments"] removed the key value pair and left only the content associated with a comment.
🌐
Stack Overflow
stackoverflow.blog › 2022 › 06 › 02 › a-beginners-guide-to-json-the-data-format-for-the-internet
A beginner's guide to JSON, the data format for the internet - Stack Overflow
Currently, you can get equivalent functionality by exporting a JavaScript Object the same as your desired JSON from a JavaScript file. ... Now this object will be stored in the constant, data, and will be accessible throughout your application using import or require statements. Note that this will import a copy of the data, so modifying the object won’t write the data back to the file or allow the modified data to be used in other files. Once you have a variable containing your data, in this example data, to access a key’s value inside it, you could use either data.key or data["key"]. Square brackets must be used for array indexing; for example if that value was an array, you could do data.key[0], but data.key.0 wouldn’t work.