View JSON as a tree: expand only the branch you need
How a JSON tree view helps with deeply nested data, how to read paths and types, and where a tree is better than a table or raw text.
Some JSON is simply deep. A cloud resource description, a build configuration, or a GraphQL response can nest objects and arrays six or seven levels down, and the one value you want is somewhere in the middle. Scrolling through formatted text works until it does not. A tree view shows the document as collapsible nodes so you can open one branch, read what is inside, and leave the rest closed. This guide covers how to read a tree, how to use it to find paths, and where it is a better choice than other views.
How a tree maps onto JSON
JSON is already a tree. An object is a node whose children are its key and value pairs. An array is a node whose children are its elements, numbered from zero. Strings, numbers, booleans, and null are leaves. A tree view draws exactly that structure, usually with a small arrow beside each object or array that you can click to open or close it.
Because objects and arrays can contain each other to any depth, the tree can get large. The point of the view is that it does not have to be displayed all at once. You see the top level, open what interests you, and the rest stays out of the way.
{
"service": "checkout",
"deploy": {
"region": "eu-west-1",
"containers": [
{
"name": "web",
"env": { "LOG_LEVEL": "info", "FEATURE_X": "false" },
"ports": [8080, 8443]
}
]
}
}Reading the path to a value
Every value in a document has a path from the root. In the example above, the log level is at deploy.containers[0].env.LOG_LEVEL: the deploy object, its containers array, the first element of that array, its env object, and finally the LOG_LEVEL key. Walking down the tree by expanding each of those nodes is the same as following the path.
That path is what you will eventually write in code or a query, so a tree is a convenient way to get it right. It also helps avoid a common off-by-one slip: array indexes start at zero, so the first container is [0], not [1]. If your code reads containers[1] and gets undefined, the tree will show you that the array has only one element.
Spotting types at a glance
A tree separates containers from leaves, and a good one distinguishes value types, for instance showing strings in quotes and numbers without. This is useful in the example, where FEATURE_X is the string "false" rather than the boolean false. In many languages the string "false" is truthy, so code that checks the value with a simple if can behave the opposite way to what the author intended.
Environment variables are inherently strings, so this is often by design. But when you see "8080" in one place and 8080 in another, such as a port number, it tells you something about how the file was generated. Noticing it in a document you did not write is usually the first step towards finding why a comparison fails.
Working through a large document
Start collapsed. Look at the top-level keys and open the one most likely to contain what you need. If you are not sure, open siblings one at a time and close each before moving on, so the screen does not fill up with branches you have already ruled out. Counting the children shown beside a node, if your viewer shows them, gives a sense of size before you open it.
A tree also makes it easier to compare how similar parts are structured. If you have several containers, open two of them and see whether their keys match. A key present in one and missing in the other is exactly the type of difference that causes environment-specific bugs.
In View My JSON, Tree is one of the free views, alongside Cards, Table, Format, and Validate. After you paste a document you can open the tree, expand the branch you care about, and keep the editor in view if you want to change a value.
Where a tree beats a table, and where it does not
A tree is the better choice for configuration files, nested API responses, and any document where the keys differ from node to node. It preserves the hierarchy, which is the information a table throws away.
It is the worse choice when you have long lists of similar records and want to compare values. Opening fifty array elements one by one to compare a field is tedious, and a table shows them all at once. A reasonable workflow is to use the tree to locate the array, then switch to a table to read it. The data stays the same under either view.
{
"data": {
"viewer": {
"repositories": {
"totalCount": 2,
"nodes": [
{ "name": "api", "stargazerCount": 12 },
{ "name": "docs", "stargazerCount": 3 }
]
}
}
}
}A tree for GraphQL-style responses
GraphQL responses are a good case for a tree because the nesting follows the query. A top-level data key holds the result, and each selected field adds another level. The example above has four levels before you reach a single repository name.
When a query returns something unexpected, the tree lets you check each level in turn. Does viewer exist, or is it null because the request was unauthenticated? Is nodes an empty array? Is totalCount larger than the number of nodes returned, which would indicate pagination? Answering these one level at a time is much quicker than scanning formatted text, and the shape of the response mirrors the shape of the query you wrote.
Pitfalls with deep documents
Very large arrays are awkward in any tree, because expanding one produces thousands of nodes. If you only need a few, copy a smaller sample into the viewer. Keys that contain dots or spaces can make a printed path misleading, so when you copy a path, read it carefully before using it in code.
Duplicate keys are another quirk. RFC 8259 says the names within an object should be unique, and that the behaviour of software receiving duplicates is unpredictable. Some parsers keep the first, some the last, and a tree built from a parsed document will show only one of them. If a value in your tree does not match the raw text, look for a repeated key.
Make the path part of your notes
When you find the field you were after, write down its full path along with an example value. In a bug report or a pull request, a path like deploy.containers[0].env.LOG_LEVEL is unambiguous, and a colleague can find it in the same tree in seconds. It is a small habit that saves a surprising amount of back and forth.