On this page

To find a value inside nested JSON arrays, start at the parsed root and describe every step to the target: select an object key, select an array element, then repeat as the structure requires. Write the access expression only after those transitions are clear. Before applying it to other records, inspect whether they contain the same containers and fields.
A path that works for one example is a useful starting point, but your application also needs decisions for empty collections, absent properties, null values, and unexpected structures. This FormatBits guide develops a small illustrative payload, traces a JavaScript expression through it, and shows how to turn that successful example into a practical inspection checklist. The sample data is invented for explanation; it does not describe a particular API.
Identify the root before following a path
JSON is a text format for representing structured data. Before tracing a response, establish whether you are looking at the JSON text or the data obtained after parsing it. JavaScript can use dot and bracket notation to access the parsed data. In the examples below, response means that parsed value; the parsing step is outside the example.
Next, identify the root. An object root lets you begin by selecting one of its keys. Accessing an element in an array root begins with an index. A JSON document can also contain a single primitive value, so do not assume that every response provides a collection to navigate.
Compare these two illustrative shapes:
{"members": [{"name": "Mira"}, {"name": "Sol"}]}
[{"name": "Mira"}, {"name": "Sol"}]
For the object-root example, the second member’s name is reached with:
response.members[1].name
For the array-root example, the equivalent selection begins immediately with an index:
response[1].name
The difference is the starting container. In the first example, members is an explicit step between the root and the array. In the second, the root already is the array.
Before continuing, answer two questions: Has this value already been parsed? What does its root contain? Record those answers alongside your example. They prevent you from building an otherwise plausible path against the wrong starting shape.
Trace one value through keys and indexes
Use a compact example with enough nesting to expose each transition. Here, an object contains a members array, and each illustrated member contains a powers array:
{
"team": "Example team",
"members": [
{
"name": "Mira",
"powers": ["glow", "glide"]
},
{
"name": "Sol",
"powers": ["shield", "echo", "spark"]
}
]
}
Suppose the target is spark. The complete JavaScript access expression for this particular payload is:
response.members[1].powers[2]
Read it from left to right, checking the container before each selection:
| Expression reached | Selection just made | Value in this example |
|---|---|---|
response |
Start at the parsed root | Object containing team and members |
response.members |
Select the members key |
Array of member objects |
response.members[1] |
Select the second element | Sol’s object |
response.members[1].powers |
Select the powers key |
Array containing three powers |
response.members[1].powers[2] |
Select the third element | spark |
The indexes express element positions: [1] selects the second member, and [2] selects the third power. Keep the ordinary-language position beside the index while you are learning or reviewing a path.
A useful debugging habit is to stop at the last intermediate value you can confirm. If you can locate Sol’s object but cannot establish what powers contains, inspect that boundary before extending the expression. This makes the unresolved question specific: you are checking one field on one selected record, rather than reconsidering the entire response.
Inspect representative elements
Treat the first successful access as evidence about that example. Before expecting the same path to work throughout a response, inspect several entries and compare the fields your application needs. The supplied analysis guidance recommends inspecting nested objects and lists before selecting fields; the same inspection habit is useful here.
Consider this separate illustrative array:
[
{"id": "a", "c": {"items": ["first"]}},
{"id": "b", "c": {"items": []}},
{"id": "c", "d": {"items": ["third"]}}
]
Two records have a c key, while another has a d key. Even the records containing c differ in whether their nested items collection has an element to select. Inspecting only the first record would leave both differences undiscovered.
Compare the boundaries your path depends on
Build a short comparison around your intended selection:
| Record | Relevant outer key | Nested collection shown | Question for the application |
|---|---|---|---|
a |
c |
One item | Should this item be selected? |
b |
c |
Empty array | What should an empty collection mean? |
c |
d |
One item under another key | Is this an accepted alternative shape? |
Do not automatically treat d as a substitute for c. Ask whether the API contract gives those fields the same role. Similar-looking structures alone do not answer that question.
Include visibly different records in your inspection, especially ones with missing target fields or empty collections. Your sample can reveal variation, but it cannot establish every shape the API may return. Keep observed examples separate from requirements you still need to verify.
Choose bracket notation for literal key names
When a dot appears in a key’s actual name, preserve that complete name as one selection. Do not assume the punctuation marks a boundary between nested objects.
This illustrative object contains two distinct destinations:
{
"profile.name": "Display label",
"profile": {
"name": "Nested label"
}
}
Bracket notation makes the literal key selection explicit:
response["profile.name"]
The nested selection follows a different path:
response.profile.name
For this example, the first expression selects Display label, and the second selects Nested label. When reviewing a confusing path, compare it with the exact key text in the payload. Ask whether you intended one key containing punctuation or two successive keys.
Keep documentation paths separate from executable expressions
Spring REST Docs supports bracket notation for identifying a key containing a literal dot. It also uses [].id to identify the id field across objects in a root array. That array-wide notation belongs to its documentation context.
A JavaScript expression such as response[1].id selects a field from one indexed record. A documentation path describing fields across an array serves a different purpose. Label examples accordingly when writing notes or handing a path to another developer: JavaScript access expression or documentation path.
If you receive an unfamiliar path from a tool, first establish which notation the tool expects. Do not assume that a path describing every matching field can be pasted into JavaScript as a single indexed selection.
Handle empty, absent, null, and unexpected values
A successful example should lead to an explicit handling plan for other responses. Keep four situations separate during that planning: an empty array, an absent property, a value explicitly shown as null, and a value whose structure differs from the one your next step expects.
The supplied material establishes path navigation but does not establish specific defensive JavaScript patterns. Use the following as a behavioral checklist; verify the implementation syntax against the language documentation and the response contract before turning it into production code.
| Situation observed | Question to resolve | Suggested handling decision |
|---|---|---|
| The expected array is empty | Is an empty collection an accepted result? | Return a clearly identified no-result outcome if accepted |
| The expected property is absent | Is omission allowed for this record? | Apply the documented omission behavior or report the missing field |
The property contains null |
What meaning does the contract assign to null? | Preserve that distinction when choosing the outcome |
| A container has an unexpected type | Does this violate the expected response shape? | Report the mismatch with the relevant path |
These are proposed application decisions, not claims about any particular API’s policies. An empty collection may be acceptable for your feature while a structurally unexpected value requires investigation. Define those outcomes deliberately instead of giving every case the same replacement value.
Review the path one boundary at a time
For response.members[1].powers[2], first confirm the expected root. Then inspect members, establish whether the second member is available, inspect that member’s powers field, and establish whether a third power is available. Stop the selection when a required boundary has not been established.
A language-neutral plan could read:
Check the expected root and members collection.
If the collection is acceptably empty, return no result.
Check that the requested member is available.
Check the member's powers field against the expected shape.
If its collection is acceptably empty, return no result.
Check that the requested power is available, then select it.
Report missing or unexpected structures according to the contract.
Also decide what should happen when a collection contains some elements but lacks the requested position. Having one power does not satisfy a request for the third. Recording this case makes the intended behavior reviewable before you choose a defensive access implementation.
Move from one element to a collection
An indexed expression answers a narrow question: which value is at this position in this example? Before expanding the code, decide whether the feature actually needs one selected value or a result for multiple records.
In the main example, response.members[1].powers[2] targets one power belonging to the second member. If the requirement instead asks for powers associated with every member, write that requirement explicitly. Changing the goal from one selection to a collection introduces decisions that the single path does not resolve.
Define the intended output first
For a member-and-power listing, a useful proposed output would preserve the relationship between each member and each selected power:
| Member from the example | Power from the example |
|---|---|
| Mira | glow |
| Mira | glide |
| Sol | shield |
| Sol | echo |
| Sol | spark |
This table specifies a desired result; it does not prescribe an iteration API. Retaining parent context makes the relationship visible when you review or display the selected values.
Before implementing collection processing, settle three questions: which member records should participate, which powers should be included, and how records with no usable powers should appear. For example, decide whether an accepted empty collection should contribute no rows or an explicit empty-state entry. Either choice needs to fit the feature’s purpose.
The supplied references do not establish a particular JavaScript iteration method or its behavior. Select and verify that implementation separately. The structural work remains useful: identify the outer collection, inspect each relevant nested boundary, and preserve the parent information your output requires.
Use readable formatting to inspect structure
Use FormatBits JSON formatting to make a dense response easier to inspect. Begin with a representative example, locate the root, and follow the visible nesting to the target. Keep the intended value in view so that formatting supports a concrete question: which selections connect this root to this value?
Readable formatting helps you compare the payload with your proposed path. For the main example, locate members, find its second element, locate that object’s powers, and find the third element. Then compare those four selections with response.members[1].powers[2].
Review syntax and expected shape separately
Treat syntax validation and response-shape review as separate tasks. A syntax check addresses the JSON text. Your shape review asks whether the response provides the root, keys, collections, and selected positions that your feature expects. Do not use a syntax result as the sole evidence that a particular nested access meets your requirements.
A practical review sequence is:
- Establish whether you have JSON text or parsed data.
- Identify the root and trace one target value.
- Compare several representative records.
- Resolve literal key names and documentation notation.
- Define outcomes for empty, absent, null, and unexpected values.
- Decide whether the feature needs one result or a collection.
Keep the smallest useful example alongside the resulting expression and handling decisions. A reviewer should be able to point to each key and index, understand why that element was selected, and see what the application should do when an expected boundary is unavailable.
Return from the formatted view to the code with those decisions recorded. Formatting makes the structure readable; the completed review gives each access step a clear purpose.