From Json.NET documentation: http://james.newtonking.com/projects/json/help/?topic=html/ConvertingJSONandXML.htm

You can force a node to be rendered as an Array by adding the attribute json:Array='true' to the XML node you are converting to JSON. Also, you need to declare the json prefix namespace at the XML header xmlns:json='http://james.newtonking.com/projects/json' or else you will get an XML error stating that the json prefix is not declared.

The next example is provided by the documentation:

xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
        <name>Alan</name>
        <url>http://www.google.com</url>
        <role json:Array='true'>Admin</role>
      </person>";

Generated output:

{
  "person": {
    "@id": "1",
    "name": "Alan",
    "url": "http://www.google.com",
    "role": [
      "Admin"
    ]
  }
}
Answer from Iván Pérez Gómez on Stack Overflow
Top answer
1 of 6
27

From Json.NET documentation: http://james.newtonking.com/projects/json/help/?topic=html/ConvertingJSONandXML.htm

You can force a node to be rendered as an Array by adding the attribute json:Array='true' to the XML node you are converting to JSON. Also, you need to declare the json prefix namespace at the XML header xmlns:json='http://james.newtonking.com/projects/json' or else you will get an XML error stating that the json prefix is not declared.

The next example is provided by the documentation:

xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
        <name>Alan</name>
        <url>http://www.google.com</url>
        <role json:Array='true'>Admin</role>
      </person>";

Generated output:

{
  "person": {
    "@id": "1",
    "name": "Alan",
    "url": "http://www.google.com",
    "role": [
      "Admin"
    ]
  }
}
2 of 6
16

Giving my +1 to Iván Pérez Gómez and providing some code here to support his answer:

Add the required json.net namespace to the root node:

private static void AddJsonNetRootAttribute(XmlDocument xmlD)
    {
        XmlAttribute jsonNS = xmlD.CreateAttribute("xmlns", "json", "http://www.w3.org/2000/xmlns/");
        jsonNS.Value = "http://james.newtonking.com/projects/json";

        xmlD.DocumentElement.SetAttributeNode(jsonNS);
    }

And to add json:Array attribute to elements found by xpath:

private static void AddJsonArrayAttributesForXPath(string xpath, XmlDocument doc)
    {
        var elements = doc.SelectNodes(xpath);



        foreach (var element in elements)
        {
            var el = element as XmlElement;

            if (el != null)
            {

                var jsonArray = doc.CreateAttribute("json", "Array", "http://james.newtonking.com/projects/json");
                jsonArray.Value = "true";
                el.SetAttributeNode(jsonArray);
            }
        }
    }

Here is a sample of a single child node as a json array:

🌐
Newtonsoft
newtonsoft.com › json › help › html › ConvertXmlToJsonForceArray.htm
Convert XML to JSON and force array
This sample reads the json:Array="true" attribute in the XML and places its value in an array when converting the XML to JSON.
Discussions

How do you represent a JSON array of strings? - Stack Overflow
This is all you need for valid JSON, right? ["somestring1", "somestring2"] More on stackoverflow.com
🌐 stackoverflow.com
Can a JSON file start with an array instead of an object?
Any valid JSON primitive is also a valid top-level JSON object. A JSON file could just as easily contain a single string, or even a single floating-point number, and still be valid. More on reddit.com
🌐 r/learnprogramming
18
11
May 25, 2024
How do I make a JSON object with multiple arrays? - Stack Overflow
I've never used JSON before so I'm not familiar with its syntax. At the moment I have multiple arrays containing different pieces of data. I would like to create one JSON object, that contains the More on stackoverflow.com
🌐 stackoverflow.com
How to write a simple array in json?
I have an array var array_list=["apple","orange","cat","dog"]; How do i write this in json? I saw tutorials in w3schools but it showed that json has name value/pairs,should we always write it i... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › JamesNK › Newtonsoft.Json › issues › 661
Empty array serialization with json:Array='true' · Issue #661 · JamesNK/Newtonsoft.Json
September 22, 2015 - string xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'> <role json:Array='true' /> </person>"; var doc = new XmlDocument(); doc.LoadXml(xml); var json = JsonConvert.SerializeXmlNode(doc); returns this json (please note role 'array', it has an empty object [{}] in it): Assert.AreEqual(@"{""person"":{""@id"":""1"",""role"":[{}]}}", json); However, we need this instead (we don't want empty object [] in the array) -and implemented a fix with unit tests passing-: Assert.AreEqual(@"{""person"":{""@id"":""1"",""role"":[]}}", json); Is there a way to achieve this in the current implementation?
Author   caglarcem
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › array
JSON Schema - array
A schema can ensure that each of the items in an array is unique. Simply set the uniqueItems keyword to true.
🌐
W3Schools
w3schools.com › js › js_json_arrays.asp
JSON Arrays
In JSON, array values must be of type string, number, object, array, boolean or null.
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
If the value at the specified position is JsonValue.TRUE this method returns true. If the value at the specified position is JsonValue.FALSE this method returns false. Otherwise this method returns the specified default value. ... Returns true if the value at the specified location in this ...
Find elsewhere
🌐
Leapcell
leapcell.io › blog › understanding-json-arrays-a-practical-guide
Understanding JSON Arrays: A Practical Guide | Leapcell
July 25, 2025 - JSON arrays store ordered, mixed-type data for structured information exchange.
🌐
W3Schools
w3schools.com › js › js_json_datatypes.asp
JSON Data Types
Values in JSON can be arrays. ... Values in JSON can be true/false.
🌐
Javatpoint
javatpoint.com › json-array
JSON Array - javatpoint
JSON Array for beginners and professionals with examples of JSON with java, json array of string, json array of numbers, json array of booleans, json srray of objects, json multidimentional array. Learn JSON array example with object, array, schema, encode, decode, file, date etc.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › what-is-json-array
What is JSON Array? - GeeksforGeeks
July 23, 2025 - For example, the array below has 5 elements in it each one of that is either true or false. ... Example: Here we assign a JSON Array of Booleans to the key boolean in jsonBooleanArray object.
🌐
JSONata
docs.jsonata.org › simple
Simple Queries · JSONata
true · JSON arrays are used when an ordered collection of values is required. Each value in the array is associated with an index (position) rather than a name, so in order to address individual values in an array, extra syntax is required to specify the index.
🌐
Oracle
docs.oracle.com › javame › 8.0 › api › json › api › com › oracle › json › JsonArray.html
JsonArray (JSON Documentation)
If the value at the specified position is JsonValue.TRUE this method returns true. If the value at the specified position is JsonValue.FALSE this method returns false. Otherwise this method returns the specified default value. ... Returns true if the value at the specified location in this ...
🌐
PHP
php.net › manual › en › function.json-decode.php
PHP: json_decode - Manual
When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects.
🌐
JAXB
javaee.github.io › tutorial › jsonp001.html
Introduction to JSON
JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines seven value types: string, number, object, array, true, false, and null.
🌐
W3Resource
w3resource.com › JSON › snippets › json-array-syntax.php
JSON Array: Syntax, Examples
JSON arrays are widely used for transmitting lists of data in APIs and configuration files due to their simplicity and compatibility across programming languages. Syntax: [ "value1", 123, true, null, ["nestedArray1", "nestedArray2"], { "key": "value" } ] Key Points: JSON arrays begin and end with square brackets.
Top answer
1 of 6
188

On the outermost level, a JSON object starts with a { and end with a }.

Sample data:

{
    "cars": {
        "Nissan": [
            {"model":"Sentra", "doors":4},
            {"model":"Maxima", "doors":4},
            {"model":"Skyline", "doors":2}
        ],
        "Ford": [
            {"model":"Taurus", "doors":4},
            {"model":"Escort", "doors":4}
        ]
    }
}

If the JSON is assigned to a variable called data, then accessing it would be like the following:

data.cars['Nissan'][0].model   // Sentra
data.cars['Nissan'][1].model   // Maxima
data.cars['Nissan'][2].doors   // 2

for (var make in data.cars) {
    for (var i = 0; i < data.cars[make].length; i++) {
        var model = data.cars[make][i].model;
        var doors = data.cars[make][i].doors;
        alert(make + ', ' + model + ', ' + doors);
    }
}

Another approach (using an associative array for car models rather than an indexed array):

{
    "cars": {
        "Nissan": {
            "Sentra": {"doors":4, "transmission":"automatic"},
            "Maxima": {"doors":4, "transmission":"automatic"}
        },
        "Ford": {
            "Taurus": {"doors":4, "transmission":"automatic"},
            "Escort": {"doors":4, "transmission":"automatic"}
        }
    }
}

data.cars['Nissan']['Sentra'].doors   // 4
data.cars['Nissan']['Maxima'].doors   // 4
data.cars['Nissan']['Maxima'].transmission   // automatic

for (var make in data.cars) {
    for (var model in data.cars[make]) {
        var doors = data.cars[make][model].doors;
        alert(make + ', ' + model + ', ' + doors);
    }
}

Edit:

Correction: A JSON object starts with { and ends with }, but it's also valid to have a JSON array (on the outermost level), that starts with [ and ends with ].

Also, significant syntax errors in the original JSON data have been corrected: All key names in a JSON object must be in double quotes, and all string values in a JSON object or a JSON array must be in double quotes as well.

See:

  • JSON specification
  • JSONLint - The JSON validator
2 of 6
23

A good book I'm reading: Professional JavaScript for Web Developers by Nicholas C. Zakas 3rd Edition has the following information regarding JSON Syntax:

"JSON Syntax allows the representation of three types of values".

Regarding the one you're interested in, Arrays it says:

"Arrays are represented in JSON using array literal notation from JavaScript. For example, this is an array in JavaScript:

var values = [25, "hi", true];

You can represent this same array in JSON using a similar syntax:

[25, "hi", true]

Note the absence of a variable or a semicolon. Arrays and objects can be used together to represent more complex collections of data, such as:

{
    "books":
              [
                {
                    "title": "Professional JavaScript",
                    "authors": [
                        "Nicholas C. Zakas"
                    ],
                    "edition": 3,
                    "year": 2011
                },
                {
                    "title": "Professional JavaScript",
                    "authors": [
                        "Nicholas C.Zakas"
                    ],
                    "edition": 2,
                    "year": 2009
                },
                {
                    "title": "Professional Ajax",
                    "authors": [
                        "Nicholas C. Zakas",
                        "Jeremy McPeak",
                        "Joe Fawcett"
                    ],
                    "edition": 2,
                    "year": 2008
                }
              ]
}

This Array contains a number of objects representing books, Each object has several keys, one of which is "authors", which is another array. Objects and arrays are typically top-level parts of a JSON data structure (even though this is not required) and can be used to create a large number of data structures."

To serialize (convert) a JavaScript object into a JSON string you can use the JSON object stringify() method. For the example from Mark Linus answer:

var cars = [{
    color: 'gray',
    model: '1',
    nOfDoors: 4
    },
    {
    color: 'yellow',
    model: '2',
    nOfDoors: 4
}];

cars is now a JavaScript object. To convert it into a JSON object you could do:

var jsonCars = JSON.stringify(cars);

Which yields:

"[{"color":"gray","model":"1","nOfDoors":4},{"color":"yellow","model":"2","nOfDoors":4}]"

To do the opposite, convert a JSON object into a JavaScript object (this is called parsing), you would use the parse() method. Search for those terms if you need more information... or get the book, it has many examples.