Understanding JSON Data
Posted on October 20, 2011JSON (JavaScript Object Notation) is the modern way of data exchange. You can find more on JSON here.
It is easy to manually create the JSON data. JSON data based on key/value pairs. These pairs can be in the form of object and array too. JSON data enclosed within curly brackets “{ }” and array objects in square “[ ] ” brackets.
Here’s the simple example:
{"key":"value"}
To see if it is valid JSON data, you need to pass this string to a JSON validator. There are several JSON validators can be found on the Internet. Here’s the one I use mostly which is http://jsonviewer.stack.hu/ . You can paste your JSON data here and it will show you the result in the hierarchy.
If you want to include an object in the JSON data with several key/value pairts, you can do it as below:
{
"key": "value",
"object": {
"key1": "value",
"key2": "value",
"key3": "value"
}
}
You noticed that the object contains 3 different keys with values. Make sure all keys should be different in an object.
You can make object arrays as well if you need multiple sets of data. If you want to put the key/values in an array you need to enclose then within “[ ]“.
{
"key": "value",
"object": {
"key1": "value",
"key2": "value",
"key3": "value"
},
"objectarray": [
{
"key1": "value"
},
{
"key1": "value"
},
{
"key1": "value",
"key2": "value"
}
]
}
Using the same technique, you can make nested arrays or objects.
{
"key": "value",
"object": {
"key1": "value",
"key2": "value",
"key3": "value"
},
"objectarray": [
{
"key1": "value"
},
{
"key1": "value"
},
{
"key1": "value",
"key2": "value"
},
{
"nestedarray": [
{
"key": "value"
},
{
"key": "value"
},
{
"key": "value"
}
]
}
]
}
The above example shows that there is another array object in the “nestedarray” object.
Hope this little tutorial can help you in getting basic understanding.