admin管理员组

文章数量:1289621

I am working on a project that has JSON format output. I need a clarity on the JSON array structure. So There are fields that are multiple entry like an array. If an element is an array but has only one value, does it still include an array node '[' in the structure?

Example:

This is a sample JSON element which is an array and has multiple values.

"Talents": [
      {
        "Items": "test"
      },
      {
        "Items": "test"
      }
    ]

If this element does not have multiple values, will it appear as below?

   "Talents": 
      {
        "Items": "test"
      }

The '[' does not appear for an array type element with single value. Can someone Pls clarify this?

I am working on a project that has JSON format output. I need a clarity on the JSON array structure. So There are fields that are multiple entry like an array. If an element is an array but has only one value, does it still include an array node '[' in the structure?

Example:

This is a sample JSON element which is an array and has multiple values.

"Talents": [
      {
        "Items": "test"
      },
      {
        "Items": "test"
      }
    ]

If this element does not have multiple values, will it appear as below?

   "Talents": 
      {
        "Items": "test"
      }

The '[' does not appear for an array type element with single value. Can someone Pls clarify this?

Share Improve this question edited Mar 13, 2021 at 17:20 lax1089 3,4733 gold badges18 silver badges39 bronze badges asked Jun 26, 2018 at 2:02 RickRick 1,5693 gold badges25 silver badges63 bronze badges 2
  • 1 Here is a brief introduction to JSON. w3schools./js/js_json_intro.asp – Fallenreaper Commented Jun 26, 2018 at 2:08
  • Typically, yes. It will still be an array with one item. But this depends on where you get this JSON content from, it should typically be stated in the documentation of whatever you're using – Adrian Commented Mar 13, 2021 at 17:25
Add a ment  | 

2 Answers 2

Reset to default 4

Single-item arrays will still include the array brackets in JSON format, as they are still arrays. In other words, there is no such native mechanism which converts single-item arrays to a non-array representation. So with your single-item example, it would be represented like this:

"Talents": [
  {
    "Items": "test"
  }
]

You can easily test this out with some simple code:

let jsonSingleItem = { "Talents": [ {"Items": "item1"} ] };
let arraySingleItem = [ {"Items": "item1"} ];

console.log(JSON.stringify(jsonSingleItem));
console.log(jsonSingleItem);
console.log(arraySingleItem);

Which yields the following output:

{"Talents":[{"Items":"item1"}]}
{ Talents: [ { Items: 'item1' } ] }
[ { Items: 'item1' } ]

So in all cases (a stringified JSON object, native JSON, and a javascript array) the single item is still in an array.

Note: It is not unmon that a consumer of an API will send data (i.e. JSON) in ways which are outside the agreed-upon contract/schema that API defines, and this (sending an object instead of a single-object array when there is just one item) is one example I have seen before. It would be up to the owner/developer of the API as to whether they build in flexibility to handle input which deviates from the API schema.

Square brackets ("[]") denotes JSONArray which in your case can access like

Talents[0]

will return

      {
        "Items": "test"
      }

In second case, curve brackets denotes an JSON object. If you want to access value of items. Than you can by

Talents.Items

OR

Talents["Items"]

will return

"Test"

for plete reference, JSON Syntax

本文标签: javascriptJSON array with single elementStack Overflow