Exemples jsonQuery
We start from the following json:
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 }
];
Initialize object:
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:
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:
3° example. add data to json if not exist:
var re2=jq.extendData({ 'user': 'carol', 'age': 40, 'active': true , 'nSon':10},users);
Result:
4° example. add data to json if not exist:
var re2_2=jq.extend({ 'user': 'tom', 'age': 41, 'active': true , 'nSon':10});
Result:
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:
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:
7° example. Select object from json:
var re5=jq.select(['user','sSon']);
Result:
7° 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:
complex json data
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);
8° example. Select object from json:
var rec1=jq2.select(['user','name']);
Result:
9° example. Where clause:
var rec6=jq2.where({
major:{'user':{"id":100}},
minor:{'user':{"id":300}, age:21},
});
Result:
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);
10° 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:
11° 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:
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);
11° example. Complex Select:
var rec16=jq4.select([{'user':["id","alias",{spouse:['name']}]}]);
Result:
12° example.More Complex Select:
rec17=jq4.select([{'user':["id","alias",{spouse:['name','age',{parent:["fatherName","fatherAge"]}]}]}]);
Result: