What is jsonQuery?.
It is an advanced jQuery library specifically designed for working with data in JSON format. It provides a powerful and flexible way to filter, query, and manipulate JSON data.
This library is particularly useful for developers working with large datasets or complex data structures in JSON. With jsonQuery, you can easily perform operations like searching, sorting, and filtering data. It supports a variety of queries.
Click here to go to the GitHub jsonQuery repository.
Click here to view a demo.
Click here to download the demo.
Click here to download the jsonQuery library.
[Expand]
Table of contents
For instance, we start from the following data in json format:
var users = [
{ 'user': 'jon', 'age': 40, 'active': true , 'nSon':10},
{ 'user': 'susan', 'age': 40, 'active': false , 'nSon':5},
{ 'user': 'james', 'age': 1, 'active': true, 'nSon':8 }
];
we can make the following query to filter and select the data we want:
var jq=new jsonQuery(users);
var re6=jq.where(
{
major:{'age':'0','nSon':4},
equal:{'active':true},
minor:{'nSon':40},
majorOrEqual:{'nSon':10},
minorOrEqual:{'nSon':40},
}).select(['user','age']);
I get the following result:
var jq=new jsonQuery(users);
[
{
"user": "jon",
"age": 40
}
]
Getting Started
1º. Add a reference to jQuery library.
2º. Add a reference to jsonQuery library.
Starting from the following data in json format:
var users = [
{ 'user': 'jon', 'age': 40, 'active': true , 'nSon':10},
{ 'user': 'susan', 'age': 40, 'active': false , 'nSon':5},
{ 'user': 'james', 'age': 1, 'active': true, 'nSon':8 }
];
You just need to initialize your object to perform queries on:
var jq=new jsonQuery(users);
Examples of queries:
Starting from the following data in json format:
var users = [
{ 'user': 'jon', 'age': 40, 'active': true , 'nSon':10},
{ 'user': 'susan', 'age': 40, 'active': false , 'nSon':5},
{ 'user': 'james', 'age': 1, 'active': true, 'nSon':8 }
];
And initializing your object to perform queries on:
var jq=new jsonQuery(users);
1° example. Simple where query to filter json:
var re=jq.where({major:{'age':20,'nSon':7},
equal:{'active':true}
});
Result:
[
{
"user":"jon",
"age":40,
"active":true,
"nSon":10
}
]
2° example. Where query to filter json:
var re1=jq.where({
major:{'age':0,'nSon':4},
equal:{'active':true},
minor:{'nSon':40},
majorOrEqual:{'nSon':10},
minorOrEqual:{'nSon':40}
});
Result:
[
{
"user": "jon",
"age": 40,
"active": true,
"nSon": 10
}
]
3° example. add data to json if not exist:
var re2=jq.extendData({ 'user': 'carol', 'age': 40, 'active': true , 'nSon':10},users);
Result:
[
{
"user": "jon",
"age": 40,
"active": true,
"nSon": 10
},
{
"user": "susan",
"age": 40,
"active": false,
"nSon": 5
},
{
"user": "james",
"age": 1,
"active": true,
"nSon": 8
},
{
"user": "carol",
"age": 40,
"active": true,
"nSon": 10
}
]
4° example. add data to json if not exist:
var re2_2=jq.extend({ 'user': 'tom', 'age': 41, 'active': true , 'nSon':10});
Result:
[
{
"user": "jon",
"age": 40,
"active": true,
"nSon": 10
},
{
"user": "susan",
"age": 40,
"active": false,
"nSon": 5
},
{
"user": "james",
"age": 1,
"active": true,
"nSon": 8
},
{
"user": "tom",
"age": 41,
"active": true,
"nSon": 10
}
]
5° example. whereOr each condition is evaluated as "or" to the following condition
var re3=jq.whereOr(
{
major:{'age':20,'nSon':7},
equal:{'active':true}
});
Result:
[
{
"user": "jon",
"age": 40,
"active": true,
"nSon": 10
},
{
"user": "susan",
"age": 40,
"active": false,
"nSon": 5
},
{
"user": "james",
"age": 1,
"active": true,
"nSon": 8
}
]
6° example. whereOr each condition is evaluated as "or" to the following condition:
var re4=jq.whereOr(
{
major:{'age':39,'nSon':10},
equal:{'user':'fred'}
});
Result:
[
{
"user": "jon",
"age": 40,
"active": true,
"nSon": 10
},
{
"user": "susan",
"age": 40,
"active": false,
"nSon": 5
}
]
7° example. Select object from json:
var re5=jq.select(['user','sSon']);
Result:
[
{
"user": "jon",
"nSon": 10
},
{
"user": "susan",
"nSon": 5
},
{
"user": "james",
"nSon": 8
}
]
8° example. Select object from json and applying where clause:
var re6=jq.where(
{
major:{'age':0,'nSon':4},
equal:{'active':true},
minor:{'nSon':40},
majorOrEqual:{'nSon':10},
minorOrEqual:{'nSon':40},
}).select(['user','age']);
Result:
[
{
"user": "jon",
"age": 40
}
]
Quering with complex json data, starting from the following data in json:
var users2 = [
{ "user": { "id": 100, "alias": "carol" }, "text": "good moring",name:"carol", age:20 },
{ "user": { "id": 130, "alias": "jon" }, "text": "bay bay",name:"jon" , age:20},
{ "user": { "id": 155, "alias": "maria" }, "text": "see you",name:"maria" , age:30},
{ "user": { "id": 301, "alias": "lucas" }, "text": "helloo ..." ,name:"lucas", age:10}
];
Initialize object:
var jq2=new jsonQuery(users2);
9° example. Select object from json
var rec1=jq2.select(['user','name']);
Result:
[
{
"user": {
"id": 100,
"alias": "carol"
},
"name": "carol"
},
{
"user": {
"id": 130,
"alias": "jon"
},
"name": "jon"
},
{
"user": {
"id": 155,
"alias": "maria"
},
"name": "maria"
},
{
"user": {
"id": 301,
"alias": "lucas"
},
"name": "lucas"
}
]
10° example. Where clause:
var rec6=jq2.where({
major:{'user':{"id":100}},
minor:{'user':{"id":300}, age:21},
});
Result:
[
{
"user": {
"id": 130,
"alias": "jon"
},
"text": "bay bay",
"name": "jon",
"age": 20
}
]
Other complex json data
var users3 = [
{ "user": { "id": 301, "alias": "sofi",spouse:{age:50, name:'sofia'} }, "text": "hello" ,name:"sofia", age:10},
{ "user": { "id": 100, "alias": "jon",spouse:{age:90, name:'jon'} }, "text": "bay bay",name:"jon", age:22 },
{ "user": { "id": 101, "alias": "carol",spouse:{age:91, name:'carol'} }, "text": "see you",name:"carol", age:22 },
{ "user": { "id": 130, "alias": "tom",spouse:{age:30, name:'tom'} }, "text": "bay",name:"tom" , age:20},
{ "user": { "id": 155, "alias": "maria",spouse:{age:40, name:'mario'} }, "text": "good bay",name:"maria" , age:30}
];
Initialize object:
var jq3=new jsonQuery(users3);
11° example. Where clause:
var rec8=jq3.where({
major:{'user':{"id":100}},
minor:{'user':{"id":300,spouse:{age:40}}, age:21},
equal:{'user':{spouse:{name:'sofia'}}},
});
Result:
12° example. whereOr each condition is evaluated as "or" to the following condition:
var rec11= jq3.whereOr(
{
major:{'user':{"id":101}},
minor:{user:{id:31,spouse:{age:91}}, age:20},
});
Result:
[
{
"user": {
"id": 301,
"alias": "sofi",
"spouse": {
"age": 50,
"name": "sofia"
}
},
"text": "hello",
"name": "sofia",
"age": 10
},
{
"user": {
"id": 130,
"alias": "tom",
"spouse": {
"age": 30,
"name": "tom"
}
},
"text": "bay",
"name": "tom",
"age": 20
},
{
"user": {
"id": 155,
"alias": "maria",
"spouse": {
"age": 40,
"name": "mario"
}
},
"text": "good bay",
"name": "maria",
"age": 30
},
{
"user": {
"id": 100,
"alias": "jon",
"spouse": {
"age": 90,
"name": "jon"
}
},
"text": "bay bay",
"name": "jon",
"age": 22
}
]
Other more complex json data
var users4 = [
{ "user": { "id": 301, "alias": "sofi",spouse:{age:50, name:'sofia',parent:{fatherName:'julio',motherName:'julia',fatherAge:60,motherAge:58}} }, "text": "hello" ,name:"sofia", age:10},
{ "user": { "id": 100, "alias": "jon",spouse:{age:90, name:'jon',parent:{fatherName:'bart',motherName:'mary',fatherAge:69, motherAge:70} }}, "text": "bay bay",name:"jon", age:22 },
{ "user": { "id": 101, "alias": "carol",spouse:{age:91, name:'carol'} }, "text": "see you",name:"carol", age:22 },
{ "user": { "id": 130, "alias": "tom",spouse:{age:30, name:'tom'} }, "text": "bay",name:"tom" , age:20},
{ "user": { "id": 155, "alias": "maria",spouse:{age:40, name:'mario'} }, "text": "good bay",name:"maria" , age:30}
];
Initialize object:
var jq4=new jsonQuery(users4);
12° example. Complex Select:
var rec16=jq4.select([{'user':["id","alias",{spouse:['name']}]}]);
Result:
[
{
"user": {
"id": 301,
"alias": "sofi",
"spouse": {
"name": "sofia"
}
}
},
{
"user": {
"id": 100,
"alias": "jon",
"spouse": {
"name": "jon"
}
}
},
{
"user": {
"id": 101,
"alias": "carol",
"spouse": {
"name": "carol"
}
}
},
{
"user": {
"id": 130,
"alias": "tom",
"spouse": {
"name": "tom"
}
}
},
{
"user": {
"id": 155,
"alias": "maria",
"spouse": {
"name": "mario"
}
}
}
]
13° example.More Complex Select:
rec17=jq4.select([{'user':["id","alias",{spouse:['name','age',{parent:["fatherName","fatherAge"]}]}]}]);
Result:
[
{
"user": {
"id": 301,
"alias": "sofi",
"spouse": {
"name": "sofia",
"age": 50,
"parent": {
"fatherName": "julio",
"fatherAge": 60
}
}
}
},
{
"user": {
"id": 100,
"alias": "jon",
"spouse": {
"name": "jon",
"age": 90,
"parent": {
"fatherName": "bart",
"fatherAge": 69
}
}
}
},
{
"user": {
"id": 101,
"alias": "carol",
"spouse": {
"name": "carol",
"age": 91
}
}
},
{
"user": {
"id": 130,
"alias": "tom",
"spouse": {
"name": "tom",
"age": 30
}
}
},
{
"user": {
"id": 155,
"alias": "maria",
"spouse": {
"name": "mario",
"age": 40
}
}
}
]
Options Reference
Starting from the following data in json format:
var users = [
{ 'user': 'jon', 'age': 40, 'active': true , 'nSon':10},
{ 'user': 'susan', 'age': 40, 'active': false , 'nSon':5},
{ 'user': 'james', 'age': 1, 'active': true, 'nSon':8 }
];
And initializing your object to perform queries on:
var myJsonObject=new jsonQuery(users);
extend: Add different json data
myJsonObject.extend({ 'user': 'tom', 'age': 41, 'active': true , 'nSon':10});
extendData: Add different json data to other json
myJsonObject.extendData({ 'user': 'tom', 'age': 41, 'active': true , 'nSon':10},otherJsonObject);
find: Find elements from json data matched with the json param
myJsonObject.find({name:'sofia'})
where: Return data matched with the json parameter. Each condition is evaluated as a "where (condition 1 and condition 2 and ,...)" clause.
myJsonObject.where(
{
major:{'age':'0','nSon':4},
equal:{'active':true},
minor:{'nSon':40},
majorOrEqual:{'nSon':10},
minorOrEqual:{'nSon':40},
});
whereOr: Return data matched with the json parameter. Each condition is evaluated as a "where (condition 1 or condition 2 or ,...)" clause.
myJsonObject.whereOr(
{
major:{'age':'0','nSon':4},
equal:{'active':true},
minor:{'nSon':40},
majorOrEqual:{'nSon':10},
minorOrEqual:{'nSon':40},
});
select: Select object from json.
myJsonObject.select(['user','sSon']);
You can select complex structures. The fields to return are always an array of field names. The fields to return can be in a complex json structure.
For instance, we start from the following data in json format:
var users4 = [
{ "user": { "id": 301, "alias": "sofi",spouse:{age:50, name:'sofia',parent:{fatherName:'julio',motherName:'julia',fatherAge:60,motherAge:58}} }, "text": "hello" ,name:"sofia", age:10},
{ "user": { "id": 100, "alias": "jon",spouse:{age:90, name:'jon',parent:{fatherName:'bart',motherName:'mary',fatherAge:69, motherAge:70} }}, "text": "bay bay",name:"jon", age:22 },
{ "user": { "id": 101, "alias": "carol",spouse:{age:91, name:'carol'} }, "text": "see you",name:"carol", age:22 },
{ "user": { "id": 130, "alias": "tom",spouse:{age:30, name:'tom'} }, "text": "bay",name:"tom" , age:20},
{ "user": { "id": 155, "alias": "maria",spouse:{age:40, name:'mario'} }, "text": "good bay",name:"maria" , age:30}
];
you can make the following complex query to select the data we want:
myJsonObject.select([{'user':["id","alias",{spouse:['name','age',{parent:["fatherName","fatherAge"]}]}]}]);
and you get the following result:
[
{
"user": {
"id": 301,
"alias": "sofi",
"spouse": {
"name": "sofia",
"age": 50,
"parent": {
"fatherName": "julio",
"fatherAge": 60
}
}
}
},
{
"user": {
"id": 100,
"alias": "jon",
"spouse": {
"name": "jon",
"age": 90,
"parent": {
"fatherName": "bart",
"fatherAge": 69
}
}
}
},
{
"user": {
"id": 101,
"alias": "carol",
"spouse": {
"name": "carol",
"age": 91
}
}
},
{
"user": {
"id": 130,
"alias": "tom",
"spouse": {
"name": "tom",
"age": 30
}
}
},
{
"user": {
"id": 155,
"alias": "maria",
"spouse": {
"name": "mario",
"age": 40
}
}
}
]
You can also make multiple linked consultations:
myJsonObject.where(
{
major:{'age':'0','nSon':4},
equal:{'active':true},
minor:{'nSon':40},
majorOrEqual:{'nSon':10},
minorOrEqual:{'nSon':40},
}).select(['user','age']);
Comments
Post a Comment