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 - OpenText
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”: 20.13, “BooleanProperty”: true, “EmptyProperty”: null } { “NestedObjectProperty”: { “Name”: “Neste Object” }, “NestedArrayProperty”: [10,20,true,40] }
🌐
W3Schools
w3schools.com › js › js_json_objects.asp
JSON Object Literals
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... JSON object literals are surrounded by curly braces {}. JSON object literals contains key...
🌐
RestfulAPI
restfulapi.net › home › json › valid key names in json
Valid Key Names in JSON - REST API Tutorial
November 5, 2023 - Use clear, consistent, and meaningful names that make the JSON data self-documenting. Choose between the camel-case (‘firstName‘) or snake-case (‘first-name‘) notations, and use it consistently. Avoid redundancy in key names. If a key name at a higher level already implies information, there’s no need to repeat it in the nested object. For example, if we have "person" as a key name, we don’t need "person_firstName" inside the object.
🌐
Squarespace
developers.squarespace.com › what-is-json
What is JSON? — Squarespace Developers
The key/value pair "bar" : "Hello" is nested inside the key/value pair "foo" : { ... }. That's an example of a hierarchy in JSON data.
🌐
W3Schools
w3schools.com › js › js_json_syntax.asp
JSON Syntax
In JSON, keys must be strings, written with double quotes:
🌐
DigitalOcean
digitalocean.com › community › tutorials › an-introduction-to-json
An Introduction to JSON | DigitalOcean
August 24, 2022 - ... { "first_name" : "Sammy", "last_name" : "Shark", "location" : "Ocean", "online" : true, "followers" : 987 } Although this is a short example, and JSON can be many lines long, this demonstrates that the format is generally set up with two curly braces (or curly brackets) that are represented ...
Find elsewhere
Top answer
1 of 2
11

You should prefer the first approach, because it is much easier and more intuitive to consume the JSON that way. If someone wanted to use your API, they would likely create model classes to deserialize into. With the first approach this is easy:

Copypublic class RootObject
{
    public List<Channel> channels { get; set; }
}

public class Channel
{
    public int id { get; set; }
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
}

In fact, you can just take the JSON and dump it into a generator tool like http://json2csharp.com/ to get these classes (that is what I did here).

In contrast, with the second approach, the keys in the JSON representing the IDs are dynamic, which a generator won't recognize as such. So you'll get something like this, which needs to be manually corrected:

Copypublic class RootObject
{
    public Channels channels { get; set; }
}

public class Channels
{
    public __invalid_type__0 __invalid_name__0 { get; set; }
}

public class __invalid_type__0
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
}

I've seen some people try to fix it like this, which will work for your one-channel example, but obviously won't scale:

Copypublic class RootObject
{
    public Channels channels { get; set; }
}

public class Channels
{
    [JsonProperty("0")]
    public Data Item0 { get; set; }
}

public class Data
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
}

To consume the JSON properly with the dynamic keys, the classes actually need to look like this:

Copypublic class RootObject
{
    public Dictionary<string, Channel> channels { get; set; }
}

public class Channel
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
}

However, the fact that you need to use a Dictionary here is not always intuitive to the casual user. I have lost count of the number of times some flavor of the question, "How can I handle dynamic keys in JSON?" is asked on StackOverflow. Do your users a favor and don't make them have to think about it.

Beyond just deserializing the JSON, the first model is also superior because the Channel object contains all the data about the channel: the id is inside the object itself. It is easy to pass around and use that way. Moreover, it is trivial to convert a List<Channel> into a Dictionary<int, Channel> later if you need to do a key lookup:

Copyvar dict = rootObject.channels.ToDictionary(ch => ch.id);

With the second approach, the id is separate from rest of the channel data, so if you wanted to pass the channel to a method which needed both, you would either have to pass two parameters or create a new model class to wrap everything together. In other words, it is more awkward to use.

Bottom line, I see no real upside to using the second approach at all. Go with the first.

2 of 2
0

If a channel is an object, not an array then you should use the first option, as you define channel specific contract. If a channel can have subsets of channels, then I suggest using the second approach as you can gain an access to specific subset via the use of a key ( which has to be unique in order to work properly ).

🌐
Online Tools
onlinetools.com › json › extract-json-keys
Extract JSON Keys – Online JSON Tools
Wrap the extracted keys in double quotes. ... In this example, we load a JSON object representing a shape with three levels of nesting. As we want to extract keys only from specific levels within this nested structure, we set the depth to "2, 3". This depth argument extracts keys from depth 2 (this is the "shape" object) and depth 3 (this is the "dimensions" object).
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;
  }
}
🌐
GitHub
github.com › sithmel › json-key-value
GitHub - sithmel/jsonaut: Convert JSON and JS object to a sequence and back · GitHub
a convenient * operator that matches any index or key as long as there is one ... It is easier to show. Here's the JSON example:
Author   sithmel
🌐
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 - 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!
Top answer
1 of 3
292
var jsonData = [{"person":"me","age":"30"},{"person":"you","age":"25"}];

for(var i in jsonData){
    var key = i;
    var val = jsonData[i];
    for(var j in val){
        var sub_key = j;
        var sub_val = val[j];
        console.log(sub_key);
    }
}

EDIT

var jsonObj = {"person":"me","age":"30"};
Object.keys(jsonObj);  // returns ["person", "age"]

Object has a property keys, returns an Array of keys from that Object

Chrome, FF & Safari supports Object.keys

2 of 3
139

[What you have is just an object, not a "json-object". JSON is a textual notation. What you've quoted is JavaScript code using an array initializer and an object initializer (aka, "object literal syntax").]

If you can rely on having ECMAScript5 features available, you can use the Object.keys function to get an array of the keys (property names) in an object. All modern browsers have Object.keys (including IE9+).

Object.keys(jsonData).forEach(function(key) {
    var value = jsonData[key];
    // ...
});

The rest of this answer was written in 2011. In today's world, A) You don't need to polyfill this unless you need to support IE8 or earlier (!), and B) If you did, you wouldn't do it with a one-off you wrote yourself or grabbed from an SO answer (and probably shouldn't have in 2011, either). You'd use a curated polyfill, possibly from es5-shim or via a transpiler like Babel that can be configured to include polyfills (which may come from es5-shim).

Here's the rest of the answer from 2011:

Note that older browsers won't have it. If not, this is one of the ones you can supply yourself:

if (typeof Object.keys !== "function") {
    (function() {
        var hasOwn = Object.prototype.hasOwnProperty;
        Object.keys = Object_keys;
        function Object_keys(obj) {
            var keys = [], name;
            for (name in obj) {
                if (hasOwn.call(obj, name)) {
                    keys.push(name);
                }
            }
            return keys;
        }
    })();
}

That uses a for..in loop (more info here) to loop through all of the property names the object has, and uses Object.prototype.hasOwnProperty to check that the property is owned directly by the object rather than being inherited.

(I could have done it without the self-executing function, but I prefer my functions to have names, and to be compatible with IE you can't use named function expressions [well, not without great care]. So the self-executing function is there to avoid having the function declaration create a global symbol.)

🌐
PostgreSQL
postgresql.org › docs › 9.6 › functions-json.html
PostgreSQL: Documentation: 9.6: JSON Functions and Operators
August 12, 2021 - Note: While the examples for the functions json_populate_record, json_populate_recordset, json_to_record and json_to_recordset use constants, the typical use would be to reference a table in the FROM clause and use one of its json or jsonb columns as an argument to the function. Extracted key values can then be referenced in other parts of the query, like WHERE clauses and target lists.
🌐
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…
🌐
RestfulAPI
restfulapi.net › home › json › json syntax
JSON Syntax - REST API Tutorial
November 3, 2023 - JSON names are on the left side of the colon. They need to be wrapped in double quotation marks, as in “name” and can be any valid string. Within each object, keys need to be unique.
🌐
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.