NinjaBoy
NinjaBoy

Reputation: 3755

json - How to properly create json array

Im new to JSON and I have to deal with a complex one.

Please see image below:

enter image description here

It has an error:

enter image description here

I don't know how to properly separate the 2 json arrays. I even tried to use : instead of , on line 18 but I still get errors. BTW, I use http://jsonlint.com/ to validate.

Upvotes: 0

Views: 3439

Answers (6)

charlietfl
charlietfl

Reputation: 171700

You have more {} than needed and will make parsing your JSON more difficult:

Structure will work a lot better like this:

{"glentries":[

    { "property1":"value", "property2" : "value",.....  "lastProperty": "value"},
    { "property1":"value", "property2" : "value",.....  "lastProperty": "value"},
    { "property1":"value", "property2" : "value",.....  "lastProperty": "value"}

 ]
}

Now glentries is an array of objects that have multiple properties to them.

 alert( glentries[0].property2 )

Upvotes: 1

istos
istos

Reputation: 2662

There are many ways to construct JSON data, but it depends on your data and the way you want to present it. Here are a couple examples - hope it helps:

{
    "glEntries": [
        {
            "object1-prop1": "one"
        },
        {
            "object2-prop1": 1,
            "object2-prop2": "two"
        },
        {
            "object3-prop1": [
                "a",
                "r",
                "r",
                "a",
                "y"
            ],
            "object3-prop1.1": "string"
        }
    ],
    "otherEntries": [
        {
            "objectx": "x"
        },
        {
            "objecty": "y"
        },
        {
            "objectz": [
                1,
                2,
                3,
                4
            ]
        }
    ],
    "oneEntry": "json"
}

Other Example:

[
    {
        "obj1-prop": 222
    },
    {
        "obj2-prop": "object2"
    },
    {
        "obj3-prop": "Object3"
    },
    [
        "a",
        "r",
        "r",
        "a",
        "y",
        777,
        888
    ],
    "string",
    178,
    {
        "objectProp": "testing123"
    }
]

Upvotes: 1

vrunoa
vrunoa

Reputation: 776

I believe the correct way to build this JSON should be:

{
  "glEntries": [
    {
        "generalLedgerId":1,
        "accountId": 34,
        "amount" : 32334.23,
        "descripction": "desc1",
        "debit" : "Yes"
    },
    {
       "generalLedgerId":2,
        "accountId": 35,
        "amount" : 323.23,
        "descripction": "desc",
        "debit" : "Yes"
    },
    ...
 ]
}

Upvotes: 1

Jonathan
Jonathan

Reputation: 986

On line 2 you gave a key, but failed to do so on line 19. You have to keep the structure.

Remove the key on line 2, they shouldn't be used for arrays in that way.

Edit: In addition, you are trying to put arrays right in objects, switch the opening and ending object marks ({}) with ([]) for arrays on your first and last line.

Upvotes: 3

Ismael Ghalimi
Ismael Ghalimi

Reputation: 3565

[
  [
    {...},
    {...},
    ...
    {...}
  ],
  [
    {...},
    {...},
    ...
    {...}
  ],
  ...
  [
    {...},
    {...},
    ...
    {...}
  ]
]

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324800

The parent structure is an Object, so it is expecting a string Key for the second array. It it's supposed to be an array of arrays, you should be using an array and not an Object.

Upvotes: 0

Related Questions