If we’re manipulating XML, we can use the XQuery or XPath query languages to find the elements we want in the XML code.
In JSON, there’s no such query language built-in natively in any runtime environment, but there’re many libraries that let us do the same thing.
In this article, we’ll look at the equivalents of XQuery or XPath with JSON.
JSPath
One library that we can use to query JSON objects is the JSPath library.
To use it, we can run:
npm i jspath
to install it.
Then we can use it to query JSON by writing:
import JSPath from "jspath";
const results = JSPath.apply(
'.automobiles{.maker === "Honda" && .year > 2009}.model',
{
automobiles: [
{ maker: "Nissan", model: "Teana", year: 2000 },
{ maker: "Honda", model: "Jazz", year: 2023 },
{ maker: "Honda", model: "Civic", year: 2007 },
{ maker: "Toyota", model: "Yaris", year: 2008 },
{ maker: "Honda", model: "Accord", year: 2011 }
],
motorcycles: [{ maker: "Honda", model: "ST1300", year: 2012 }]
}
);
console.log(results);
We call JSPath.apply
to query the object we have in the 2nd argument.
The first argument is a string with the query.
We search for items in the automobiles
array with maker
equals 'Honda'
and year
bigger than 2009.
And we get the model
property of each object.
Therefore, result
is:
["Jazz", "Accord"]
It also supports wildcard queries with *
.
For instance, we can write:
import JSPath from "jspath";
const results = JSPath.apply(
'.automobiles{.maker === "Honda" && .year > 2009}.*',
{
automobiles: [
{ maker: "Nissan", model: "Teana", year: 2000 },
{ maker: "Honda", model: "Jazz", year: 2023 },
{ maker: "Honda", model: "Civic", year: 2007 },
{ maker: "Toyota", model: "Yaris", year: 2008 },
{ maker: "Honda", model: "Accord", year: 2011 }
],
motorcycles: [{ maker: "Honda", model: "ST1300", year: 2012 }]
}
);
console.log(results);
Then result
is:
["Honda", "Jazz", 2023, "Honda", "Accord", 2011]
We get all the property values of matched result objects with the *
operator.
JSONPath
Another library that we can use to query JSON objects is to use the JSONPath library.
To install it, we run:
npm i jsonpath
Then we can use it by writing:
const jp = require("jsonpath");
const persons = [
{ name: "James", age: 36 },
{ name: "Mary", age: 34 },
{ name: "Jane", age: 35 },
{ name: "Bob", age: 28 }
];
const names = jp.query(persons, "$..name");
console.log(names);
We call jp.query
to get the name
property from each entry of the persons
array with the “$..name”
query.
$
means the root object.
..
means we find the properties recursively.
Therefore, we have:
["James", "Mary", "Jane", "Bob"]
as a result of names
.
We can also query for objects that meets a given condition by writing:
const jp = require("jsonpath");
const obj = {
persons: [
{ name: "James", age: 36 },
{ name: "Mary", age: 34 },
{ name: "Jane", age: 35 },
{ name: "Bob", age: 28 }
]
};
const names = jp.query(obj, "$..persons[?(@.age>30)]");
console.log(names);
We call jp.query
to find objects in the persons
array property with age
bigger than 30.
So we get:
[
{
"name": "James",
"age": 36
},
{
"name": "Mary",
"age": 34
},
{
"name": "Jane",
"age": 35
}
]
as the value of names
.
Conclusion
We can use the JSPath and JSONPath libraries to query for items in JavaScript objects.