Home > Enterprise >  Making a search in json file by query
Making a search in json file by query

Time:04-20

i want to make a search in a json file by query Example: My json file:

[{
"Id":1,
"name":"test"
},
{
"Id":2,
"name":"test"
}]

And if i search {"name":"test"} i get the full objects whos name is test

ADD: I misspoke its the user who enter the query how can i get the key and the value from the query {key:value} and the user can entre multiple keys and values (like the search in atlas mongodb) UP!!

CodePudding user response:

As per your question you are using JSON inside of an array. So you can simply filter the array to find JSON which contains name="test"

Follow the below step:

let userArray = [{"Id":1,"name":"test"},{"Id":2,"name":"test"}]

const filteredUser = userArray.filter((user) => user.name === "test")

CodePudding user response:

Try something simple:

var myjson ='[{"Id":1,"name":"test"},{"Id":2,"name":"test"},{"Id":3,"name":"nottest"}]';
var slag = JSON.parse(myjson).filter(doc => doc.name === 'test');
console.log(slag);

  • Related