Skip to content Skip to sidebar Skip to footer

Nested Json Object

I am very much new to this JSON objects. So I need some helps from you guys. I have to create a JSON object and that object has two children. One child has some children. If my que

Solution 1:

Using the HTML tree, you will get the followin JSON representation for the PRODUCT_LIST:

[{"CATEGORY_ID":"A","PRODUCT_DETAILS":[{"PRODUCT_ID":"A.1","PRODUCT_NAME":"AAAA1111","UNIT_COST":0.1}]},{"CATEGORY_ID":"B","PRODUCT_DETAILS":[{"PRODUCT_ID":"B.1","PRODUCT_NAME":"BBBBB1111","UNIT_COST":0.2}]}]

However, your JavaScript produces a nearly valid JSON string, which is assigned to the product variable. You could use JSON.parse() on it, but that's too complicated. As the JavaScriptObjectNotation is a subset of the Javascript object literal syntax, you should directly assign the object.

In your JSON, you have some braces too much around the category objects. You can check that e.g. with http://jsonformatter.curiousconcept.com/. Corrected script:

var product = {
   "products_list":[
      {
         "category_id":"A",
         "product_details":[
            {
               "product_id":"A.1",
               "product_name":"AAAA1111",
               "unit_cost":"A1"
            },
            {
               "product_id":"A.2",
               "product_name":"AAAA2222",
               "unit_cost":"A2"
            },
            {
               "product_id":"A.3",
               "product_name":"AAAA3333",
               "unit_cost":"A3"
            },
            {
               "product_id":"A.4",
               "product_name":"AAAA4444",
               "unit_cost":"A4"
            },
            {
               "product_id":"A.5",
               "product_name":"AAAA5555",
               "unit_cost":"A5"
            },
            {
               "product_id":"A.6",
               "product_name":"AAAA6666",
               "unit_cost":"A6"
            },
            {
               "product_id":"A.7",
               "product_name":"AAAA7777",
               "unit_cost":"A7"
            },
            {
               "product_id":"A.8",
               "product_name":"AAAA8888",
               "unit_cost":"A8"
            },
            {
               "product_id":"A.9",
               "product_name":"AAAA9999",
               "unit_cost":"A9"
            },
            {
               "product_id":"A.0",
               "product_name":"AAAA0000",
               "unit_cost":"A0"
            }
         ]
      },
      {
         "category_id":"A",
         "product_details":[
            {
               "product_id":"A.1",
               "product_name":"AAAA1111",
               "unit_cost":"A1"
            },
            {
               "product_id":"A.2",
               "product_name":"AAAA2222",
               "unit_cost":"A2"
            },
            {
               "product_id":"A.3",
               "product_name":"AAAA3333",
               "unit_cost":"A3"
            },
            {
               "product_id":"A.4",
               "product_name":"AAAA4444",
               "unit_cost":"A4"
            },
            {
               "product_id":"A.5",
               "product_name":"AAAA5555",
               "unit_cost":"A5"
            },
            {
               "product_id":"A.6",
               "product_name":"AAAA6666",
               "unit_cost":"A6"
            },
            {
               "product_id":"A.7",
               "product_name":"AAAA7777",
               "unit_cost":"A7"
            },
            {
               "product_id":"A.8",
               "product_name":"AAAA8888",
               "unit_cost":"A8"
            },
            {
               "product_id":"A.9",
               "product_name":"AAAA9999",
               "unit_cost":"A9"
            },
            {
               "product_id":"A.0",
               "product_name":"AAAA0000",
               "unit_cost":"A0"
            }
         ]
      }
   ]
}

Solution 2:

All identifiers should be in quotes. By indexing by category id and making the categories arrays of objects I think you get all the features and capabilities of your data structure and make the solution very flexible.

var category = {
  "A": [
    { "product_id": "A.1", "product_name": "AAAA1111", "unit_cost": 0.1 },
    { "product_id": "A.2", "product_name": "AAAA2222", "unit_cost": 0.2 },
  ],
  "B": [
    { "product_id": "B.1", "product_name": "BBBB1111", "unit_cost": 0.2 },
    { "product_id": "B.2", "product_name": "BBBB2222", "unit_cost": 0.4 },
  ]
};

Solution 3:

This is a bit of a confusing question, but I'd probably have JSON something like this

vardata = [
  {
    category_id: 'A',
    product_id: 'A.1',
    product_name: 'AAAA1111',
    unit_cost: 0.1
  },
  {
    category_id: 'B',
    product_id: 'B.1',
    product_name: 'BBBB1111',
    unit_cost: 0.2
  }
];

And then filter it by category when displaying. You might find http://underscorejs.org/ useful for filtering your results

Post a Comment for "Nested Json Object"