0

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 question is confusing just refer the following diagram. It is similar to nested lists. Example:

PRODUCT_LIST

<ul>
    <li>CATEGORY_ID - A</li>
    <li>PRODUCT_DETAILS
        <ul>
            <li>PRODUCT_ID - A.1</li>
            <li>PRODUCT_NAME - AAAA1111</li>
            <li>UNIT_COST - 0.1</li>
        </ul>
    </li>
    <li>CATEGORY_ID - B</li>
    <li>PRODUCT_DETAILS
        <ul>
            <li>PRODUCT_ID - B.1</li>
            <li>PRODUCT_NAME - BBBBB1111</li>
            <li>UNIT_COST - 0.2</li>
        </ul>
    </li>
</ul>

I tried to do but got JavaScript errors. Can any one please help to create JSON for the above mentioned diagram. Note: One Category has more than one product details. It means PRODUCT_ID, PRODUCT_NAME and UNIT_COST will be repeating to more than one time for a particular CATEGORY_ID.

The JavaScript code I have tried

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"}' +
                            ']' + 
                        '}' + 
                    '}' + 
                ']' + 
            '}';
6
  • Please show us your JavaScript, not some HTML list. Commented Aug 22, 2012 at 10:32
  • Please refer my updated question with the Javascript I have tried.. Thanks in advance... Commented Aug 22, 2012 at 10:37
  • Is there a reason you are using a string containing JSON instead of just an object literal? Don't make it unnecessarily difficult for yourself. Commented Aug 22, 2012 at 10:42
  • Yes reason. It is similar to normalization. our customer does not need to use database, instead needs to store in JSON.to avoid the repeat values I tried. Just look the code. var product = '{"products_list":[' + '{"category_id":"A","product_id":"A.1","product_name":"AAAA1111", "unit_cost":"A1"},' + '{"category_id":"A","product_id":"A.2","product_name":"AAAA2222", "unit_cost":"A2"},' + '{"category_id":"B","product_id":"B.1","product_name":"BBBB1111", "unit_cost":"B1"},' + '{"category_id":"B","product_id":"B.6","product_name":"BBBB6666", "unit_cost":"B6"}]}'; To avoid category repeating.. Commented Aug 22, 2012 at 10:50
  • Thanks for the efforts given for my questions. I have got the answer from "Bergi".. :) Commented Aug 22, 2012 at 11:04

3 Answers 3

3

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"
            }
         ]
      }
   ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

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 },
  ]
};

1 Comment

Thanks for the efforts given for my questions. I have got the answer from "Bergi".. :)
0

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

var data = [
  {
    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

2 Comments

He seems to be asking about general object syntax in JS from the notes on the question
Thanks for the efforts given for my questions. I have got the answer from "Bergi".. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.