{
"firstName": "Jonathan",
"lastName": "Freeman",
"loginCount": 4,
"isWriter": true,
"worksWith": ["Spantree Technology Group", "InfoWorld"],
"pets": [
{
"name": "Lilly",
"type": "Raccoon"
}
]
}
You'll notice it is only made up of mainly brackets, colons, commas, double quotes, and text. There is always the main { } around everything.
Keys and values - JSON is made up of keys and values, or key/value pairs. A key is always a string enclosed in quotation marks, usually describes what the value is. A value can be a string, number, boolean expression, array, or object. Keys are case sensitive. A key value pair follows a specific syntax, with the key followed by a colon followed by the value. Key/value pairs are comma separated. Let's take this example:
{
"person" : {
"name" : "John",
"birthday" : "1975-01-31",
"height" : "67"
}
}
The key/value pair "name" : "John" is nested inside the key/value pair "person" : { ... }. That's an example of a hierarchy in JSON data. An Object can have any combination of values inside of it, so it could have another object, string, number, etc as a value for a key/value pair.
Arrays - the "worksWith" key has a square bracket to determine it is an array. The value is multiple strings this time. Each array element has to be the same thing. There can be an array of numbers, strings, objects, etc.
{
"firstName": "John",
"lastName": "Doe",
"loginCount": 7,
"worksWith": ["Spantree Technology Group", "InfoWorld", "Test"]
}
Here is an example of an array of objects:
{
"firstName": "John",
"lastName": "Smith",
"birthday": "1975-01-31",
"spouse": {
"firstName": "Mary",
"lastName": "Smith"
},
"addresses": [
{
"description": "home",
"street": "123 Peachtree Ln",
"city": "Atlanta",
"state": "GA",
"postalCode": 30305
},
{
"description": "work",
"street": "456 Peachtree St",
"city": "Atlanta",
"state": "GA",
"postalCode": 30305
}
],
"phoneNumbers": [
{
"description": "home",
"number": "404-555-1234"
},
{
"description": "mobile",
"number": "678-555-1234"
}
]
}
The key "addresses" is an array, denoted by the [ ] and inside are the objects denoted by { } and there are multiple objects, so they are separated by the comma.
When building the JSON profile, there aren't many other options besides what kind of element type to choose. Your JSON profile can be as complicated or as simple as you'd like. The only other thing is a repeating array and absolute array. Repeating arrays repeat one kind of element and allow you to repeat once or unlimited times while absolute arrays allow for different kinds of elements but you have to define every instance and type of element in that absolute array in the same order the data is coming in. You only have to define the element(s) one time each in a repeating array.
Repeating array of numbers: [1, 2, 3, 4]
Repeating array of objects:
{
"number": [{
"upc": "123",
"price": 1.99
}, {
"upc": "345",
"price": 2.99
}, {
"upc": "456",
"price": 5.99
}
]
}
This is how the array of numbers would be defined in a profile, notice how there's only one simple element number. It is understood the repeating array has the same element so you only need to define it once. Which also means, if you are mapping to this array, your data needs to be in a repeating format too to map properly.
This is how the array of objects would look like defined in the profile,
Absolute array (contains different elements, number, string, object):
[
1,
“Orange Juice”, {
"upc": 123,
"price": 1.99
},
"name": "Alan"
]