Arrays

When we reference an Array field, the value of the field may be an array that contains several elements, but we may want only a few of those elements. For example, we may want to reference only the first element; only the last element; or perhaps the first, second, third, and last elements. We can reference a specific element simply by using the index of the element within square brackets (the index is 0-based). So let us consider a modified version of the Record above:


{
        "name": "John Doe",
        "addresses": [
                "work": {
                        "number": "123",
                        "street": "5th Avenue",
                        "city": "New York",
                        "state": "NY",
                        "zip": "10020"
                },
                "home": {
                        "number": "456",
                        "street": "116th Avenue",
                        "city": "New York",
                        "state": "NY",
                        "zip": "11697"
                }
        ]
}

We can now reference the first element in the addresses array by using the RecordPath /addresses[0]. We can access the second element using the RecordPath /addresses[1]. There may be times, though, that we don't know how many elements will exist in the array. So we can use negative indices to count backward from the end of the array. For example, we can access the last element as /addresses[-1] or the next-to-last element as /addresses[-2]. If we want to reference several elements, we can use a comma-separated list of elements, such as /addresses[0, 1, 2, 3]. Or, to access elements 0 through 8, we can use the range operator (..), as in /addresses[0..8]. We can also mix these, and reference all elements by using the syntax /addresses[0..-1] or even /addresses[0, 1, 4, 6..-1]. Of course, not all of the indices referenced here will match on the Record above, because the addresses array has only 2 elements. The indices that do not match will simply be skipped.