2

I have this data sample that I need to put into a JSON format.

What's the best way/structure to do that? If it helps, I'll be developing an angular product selection tool for this.

Item 1: Federation Phaser
Options:
  | FORM FACTOR   | PRICE |
  | Compact             | $545  |
  | Pistol Grip          | $600  |

Item 2: Sith Lightsaber
Options:
  | BLADE COLOR | BLADE COUNT | PRICE |
  | Red                    | Single                 | $1000 |
  | Red                    | Double               | $1750 |
  | Blue                   | Single                 | $1125 |
  | Blue                   | Double                | $1875 |
  | Green                | Single                  | $1250 |
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Lz430
  • 307
  • 2
  • 7
  • 16
  • Possible duplicate of [Get Table data in json format](https://stackoverflow.com/questions/44334866/get-table-data-in-json-format) – ivan_pozdeev Jun 11 '18 at 20:59

1 Answers1

2

JSON is formed by name/value pairs and surrounded by curly braces {}. The name/value pair are separated by commas and the values themselves can be JSON objects or arrays.

Example 1 (Simple):

{
  "fruit1": "apple",
  "fruit2": "pear"
}

Example 2 (more complex):

{
  "fruitBasket1": { "fruit1": "apple", "fruit2": "pear"},
  "fruitBasket2": { "fruit1": "grape", "fruit2": "orange"}
}

For your example, you could construct the JSON as follows with an array:

{
    "item": {
        "name": "Federation Phaser",
        "options": [
            {
                "form": "compact",
                "price": "$545"
            },
            {
                "form": "Pistol Grip",
                "price": "$600"
            }
        ]
    },
    "item2": {
        "name": "Sith Lightsaber",
        "options": [
            {
                "bladeColor": "red",
                "count": "single",
                "price": "$1000"
            },
            {
                "bladeColor": "blue",
                "count": "double",
                "price": "$1875"
            }
        ]
    }
}

If you want to have a variable number of "items" you could put them into an array too. For example:

{
    "items": [
        {
            "name": "Federation Phaser",
            "options": [{
                    "form": "compact",
                    "price": "$545"
                },
                {
                    "form": "Pistol Grip",
                    "price": "$600"
                }
            ]
        },
        {
            "name": "Sith Lightsaber",
            "options": [{
                    "bladeColor": "red",
                    "count": "single",
                    "price": "$1000"
                },
                {
                    "bladeColor": "blue",
                    "count": "double",
                    "price": "$1875"
                }
            ]
        }
    ]
}
Geoffrey Bourne
  • 560
  • 4
  • 13
  • That's exactly what I needed. I just didn't know how to add the options into an array like that. Thank you! – Lz430 Jun 11 '18 at 20:50