In projects with front-end and back-end separation, it is often necessary for the front-end to send json data to the back-end, and the back-end processes the data through json operations and then returns it to the front-end. In this process, it involves querying json data and nested queries, which is also one of the common operations of nodejs.
In nodejs, using the JSON
object can conveniently operate on json data. The following will introduce how to perform json nested query in nodejs.
Suppose there is the following json data:
{ "name": "Tom", "age": 20, "address": { "province": "Guangdong", "city": "Shenzhen", "district": "Futian" }, "skills": [ { "name": "Java", "level": "Expert" }, { "name": "JavaScript", "level": "Intermediate" } ] }
Suppose we want to query the city information in Tom’s address, we can use the following code:
const json = {...}; // 假设json数据如上 const city = json.address.city; console.log(city); // Shenzhen
In the above code, we pass json.address.city
method can directly obtain city information.
Suppose we also want to query the skill name of Tom’s first skill, you can use the following code:
const json = {...}; // 假设json数据如上 const skillName = json.skills[0].name; console.log(skillName); // Java
In this code, we use a nested query to obtain Tom’s first skill A skill name. First use json.skills
to get Tom’s skills
attribute, then use [0]
to get the first skill, and finally use .name
Get the skill name.
But what should we do if we don’t know the specific structure of json data, or need to query based on user input?
We can use recursion to perform deep traversal queries on json data. The following is an example of using recursion to query json:
function searchJson(json, targetKey, targetValue) { for (let key in json) { if (typeof json[key] === 'object') { searchJson(json[key], targetKey, targetValue); } else { if (key === targetKey && json[key] === targetValue) { console.log(json); } } } }
This function accepts three parameters, which are json data, target attribute name and target attribute value. Inside the function, use a for in loop to perform a deep traversal query on the json data. If the attribute value is an object, it will be traversed recursively. Otherwise, it will be judged whether the attribute name is equal to the target attribute name and whether the attribute value is equal to the target attribute value. If it matches, it will be printed out. The json object.
For example, if we want to query all objects named Tom in json, we can use the following code:
searchJson(json, 'name', 'Tom');
This code will deeply traverse the json data and return all objects named Tom. object.
Summary:
Manipulating json data in nodejs is a very common operation. This article takes common json nested queries as an example to introduce how to query and query json in nodejs. Nested query operations. In actual development, we can choose different methods to operate and process json data according to specific situations.
The above is the detailed content of nodejs query json nesting. For more information, please follow other related articles on the PHP Chinese website!