Fork me on GitHub

Utility plugins

jsonQ

jsonQ is a light weight and feature-rich javascript library to make your world around JSON fast and easy. For a JSON, it makes traversing (you don’t need to make loop inside loop), extracting data, manipulating it very simple.  It also provide some utilty methods , we need while working with JSON which directly works with a JSON.

jsonQ followed jQuery mantra "write less do more" and some of coding technuiques of it. Thanks to jQuery foundation. :)

A simple example.
To get value associated with key "name" from a JSON stored in a variable "jsonObj" we need to write as simple as.

jsonQ(jsonObj).find("name").value();
Your name key can be at any level within JSON it will find it and return in array.

Download

JSON used for examples


var jsonObj={
    "fathers": [
        {
            "age": 44,
            "name": "James Martin",
            "daughters": [
                {
                    "age": 24,
                    "name": "Michelle",
                    "husband": {
                        "age": 30,
                        "name": "Matthew"
                    }
                },
                {
                    "age": 30,
                    "name": "Angela",
                    "husband": {
                        "age": 23,
                        "name": "William"
                    }
                }
            ]
        },
        {
            "age": 47,
            "name": "David Thompson",
            "daughters": [
                {
                    "age": 20,
                    "name": "Amy",
                    "husband": {
                        "age": 26,
                        "name": "Edward"
                    }
                },
                {
                    "age": 20,
                    "name": "Dorothy",
                    "husband": {
                        "age": 23,
                        "name": "Timothy"
                    }
                }
            ]
        },
        {
            "age": 56,
            "name": "Thomas Young",
            "daughters": [
                {
                    "age": 22,
                    "name": "Sharon",
                    "husband": {
                        "age": 23,
                        "name": "Jason"
                    }
                },
                {
                    "age": 22,
                    "name": "Carol",
                    "husband": {
                        "age": 23,
                        "name": "William"
                    }
                },
                {
                    "age": 20,
                    "name": "Brenda",
                    "husband": {
                        "age": 30,
                        "name": "Timothy"
                    }
                }
            ]
        },
        {
            "age": 53,
            "name": "Jason Martinez",
            "daughters": [
                {
                    "age": 19,
                    "name": "Jessica",
                    "husband": {
                        "age": 24,
                        "name": "Daniel"
                    }
                }
            ]
        },
        {
            "age": 51,
            "name": "Thomas Gonzalez",
            "daughters": [
                {
                    "age": 23,
                    "name": "Brenda",
                    "husband": {
                        "age": 30,
                        "name": "George"
                    }
                },
                {
                    "age": 30,
                    "name": "Dorothy",
                    "husband": {
                        "age": 23,
                        "name": "Brian"
                    }
                }
            ]
        },
        {
            "age": 41,
            "name": "James Lee",
            "daughters": [
                {
                    "age": 20,
                    "name": "Sarah",
                    "husband": {
                        "age": 24,
                        "name": "Frank"
                    }
                },
                {
                    "age": 21,
                    "name": "Carol",
                    "husband": {
                        "age": 28,
                        "name": "Larry"
                    }
                }
            ]
        },
        {
            "age": 58,
            "name": "Kenneth Brown",
            "daughters": [
                {
                    "age": 23,
                    "name": "Ruth",
                    "husband": {
                        "age": 24,
                        "name": "Brian"
                    }
                },
                {
                    "age": 18,
                    "name": "Lisa",
                    "husband": {
                        "age": 24,
                        "name": "Scott"
                    }
                },
                {
                    "age": 27,
                    "name": "Sandra",
                    "husband": {
                        "age": 31,
                        "name": "Charles"
                    }
                }
            ]
        },
        {
            "age": 50,
            "name": "Thomas Lee",
            "daughters": [
                {
                    "age": 27,
                    "name": "Patricia",
                    "husband": {
                        "age": 30,
                        "name": "Scott"
                    }
                },
                {
                    "age": 21,
                    "name": "Jennifer",
                    "husband": {
                        "age": 23,
                        "name": "George"
                    }
                }
            ]
        },
        {
            "age": 50,
            "name": "Robert Anderson",
            "daughters": [
                {
                    "age": 24,
                    "name": "Angela",
                    "husband": {
                        "age": 23,
                        "name": "James"
                    }
                }
            ]
        }
    ]
}

Initialize jsonQ

To bind jsonQ method in any JSON , initialize it with like this.

var jsonQobj=jsonQ(jsonObj);

Traverse and Filter Method

find

Find the key on descendants of each element in the current set of matched elements in a jsonQ object.
Syntax: .find(key[,qualifier])
Return: this (a jsonQ object, so it can be chained).
Parameters
key (string) :Key which you want to find on a current set in jsonQ object.
qualifier(string /function /JSON) :A qualifier condition. So find will return only those element who match qualifier condition. See more
Example

var family = jsonQ(jsonObj),
    //to find all the name
    name = family.find('name');

//to print list of all name
console.log(name.value());

//will give you
["James Martin", "Michelle", "Matthew", "Angela", "William", "David Thompson", "Amy", "Edward", "Dorothy", "Timothy", "Thomas Young", "Sharon", "Jason", "Carol", "William", "Brenda", "Timothy", "Jason Martinez", "Jessica", "Daniel", "Thomas Gonzalez", "Brenda", "George", "Dorothy", "Brian", "James Lee", "Sarah", "Frank", "Carol", "Larry", "Kenneth Brown", "Ruth", "Brian", "Lisa", "Scott", "Sandra", "Charles", "Thomas Lee", "Patricia", "Scott", "Jennifer", "George", "Robert Anderson", "Angela", "James"]

//finding name with qualifier
//name which start with m
var name = family.find('name', function () {
    return this[0].toLowerCase() == 'm'
});

console.log(name.value());
//will return 
["Michelle", "Matthew"]

//to find all the husband's name whose age is grater than 24
var name = family.find('husband', function () {
    return this.age > 24;
}).find('name');

console.log(name.value());
//will return 
["Matthew", "Edward", "Timothy", "George", "Larry", "Charles", "Scott"]

sibling

Find the key on siblings of each element in the current set of matched elements in a jsonQ object.
Syntax: .sibling(key[,qualifier])
Return: this (a jsonQ object, so it can be chained).
Parameters
key (string) :Sibling key which you want to select for on a current set in jsonQ object.
qualifier(string /function /JSON) :A qualifier condition. So siblings will return only those element who match qualifier condition. See more
Example

var family = jsonQ(jsonObj),
    //to find all the name which start with a
    name = family.find('name', function () {
        return this[0].toLowerCase() == 'a'
    });

//to find the age of filtered list of person;
var age = name.sibling('age');

console.log(age.value());
//will return 
[30, 20, 24]

parent

Find the parent (weather its a object or array) of each element in the current set of matched elements in a jsonQ object.
Syntax: .parent()
Return: this (a jsonQ object, so it can be chained).
Example

var family = jsonQ(jsonObj);

//if you want to get name and age of all person whose age is greated then or equal to 30
var personObj = family.find('age', function () {
    return this > = 30;
}).parent();

personObj.each(function (index, path, value) {
    console.log(value.name + '  ' + value.age);
});
//will print

James Martin  44

Matthew  30

Angela  30

David Thompson  47

Thomas Young  56

Timothy  30

Jason Martinez  53

Thomas Gonzalez  51

George  30

Dorothy  30

James Lee  41

Kenneth Brown  58

Charles  31

Thomas Lee  50

Scott  30

Robert Anderson  50

closest

It returns the first element which match the provided key while traversing up in JSON for each element in the current set of matched elements in a jsonQ object.
Syntax: .closest(key)
Return: this (a jsonQ object, so it can be chained).
Parameters
key (string) :Closest ancestor key which you want to find in the current set of matched elements in a jsonQ object.
Example

var family=jsonQ(jsonObj);


//to find all father's name and age whose any of daughter's husband age is greater than or equal to 30
var	husband= family.find('husband',function(){
return this.age>
=30;
});

var father=husband.closest('daughters').parent();

father.each(function(index,path,value){
console.log(value.name +'  '+value.age);
});

//will return
James Martin  44

Thomas Young  56

Thomas Gonzalez  51

Kenneth Brown  58

Thomas Lee  50                    


filter

Filter the current set of matched element in jsonQ object and returns only those who are qualified in qualifier condition.
Syntax: .filter(qualifier)
Return: this (a jsonQ object, so it can be chained).
Parameters
qualifier(string /function /JSON) :A qualifier condition. So filter will return only those element who match qualifier condition. See more
Example

var family=jsonQ(jsonObj),
//to find all the husband 
husband= family.find('husband');

//to filter husband whose age is 30.
var	filterdHub=husband.filter({"age":30});


filterdHub.each(function(index,path,value){
console.log(value.name +'  '+value.age);
});

//will return 

Matthew  30

Timothy  30

George  30

Scott  30


Manipulation Method

value

It set value for each element of current set of matched element in jsonQ object.
Syntax: .value(data,[clone])
Return: this (a jsonQ object, so it can be chained but need to be refreshed before any other method is called).
Parameters
data(any valid json data type / function) :data can be a valid json data type or can be a callback function. If it is current value be set as first parameter of callback function and processed data should be returned. clone(Boolean -true, false - (default true)) :If data is an object type and if clone is false it will put the exact object (by refrence) else if true, it will clone the data.
Example

var family = jsonQ(jsonObj);

//to set all husband age to  30;
family.find('husband').find('age').value(30);


//to add 2 years on all husband's age
family.find('husband').find('age').value(function (data) {
    return data + 2;
});

append

It append value in each element of current set of matched element in jsonQ object. The value of each element of current set should be string or array.
Syntax: .append(data,[clone])
Return: this (a jsonQ object, so it can be chained but need to be refreshed before any other method is called).
Parameters
data(any valid json data type) :data you want to append on value of current set of matched element.
clone(Boolean -true, false - (default true)) :If data is an object type and if clone is false it will append the exact object (by refrence) else if true, it will clone the data and append it.
Example

var family = jsonQ(jsonObj);

//to add .. on all husband's name;
var name = family.find('husband').find('name');

name.append('..');

console.log(name.value());

// will return
["Matthew..", "William..", "Edward..", "Timothy..", "Jason..", "William..", "Timothy..", "Daniel..", "George..", "Brian..", "Frank..", "Larry..", "Brian..", "Scott..", "Charles..", "Scott..", "George..", "James.."]

//to append a daughter in all daughter's key
var newDaughter = {
    "age": 24,
    "name": "Michelle",
    "husband": {
        "age": 30,
        "name": "Matthew"
    }
}

family.find('daughters').append(newDaughter);

prepend

It prepend value in each element of current set of matched element in jsonQ object. The value of each element of current set should be string or array.
Syntax: .prepend(data,[clone])
Return: this (a jsonQ object, so it can be chained but need to be refreshed before any other method is called).
Parameters
data(any valid json data type) :data you want to prepend on value of current set of matched element.
clone(Boolean -true, false - (default true)) :If data is an object type and if clone is false it will prepend the exact object (by refrence) else if true, it will clone the data and prepend it.
Example

var family = jsonQ(jsonObj);

//to add Mrs on first of all husband's name;
var name = family.find('husband').find('name');

name.prepend('Mrs. ');

console.log(name.value());

// will return
["Mrs. Matthew", "Mrs. William", "Mrs. Edward", "Mrs. Timothy", "Mrs. Jason", "Mrs. William", "Mrs. Timothy", "Mrs. Daniel", "Mrs. George", "Mrs. Brian", "Mrs. Frank", "Mrs. Larry", "Mrs. Brian", "Mrs. Scott", "Mrs. Charles", "Mrs. Scott", "Mrs. George", "Mrs. James"]

//to prepend a daughter in all daughter's key
var newDaughter = {
    "age": 24,
    "name": "Michelle",
    "husband": {
        "age": 30,
        "name": "Matthew"
    }
}

family.find('daughters').prepend(newDaughter);

appendAt

It append value at specified index in each element of current set of matched element in jsonQ object. The value of each element of current set should be string or array.
Syntax: .appendAt(index,data,[clone])
Return: this (a jsonQ object, so it can be chained but need to be refreshed before any other method is called).
Parameters
index(number - start from 0) :Index at which you want to append the data. data(any valid json data type) :data you want to append at specified position on value of current set of matched element.
clone(Boolean -true, false - (default true)) :If data is an object type and if clone is false it will append the exact object (by refrence) else if true, it will clone the data and append it.
Example

var family = jsonQ(jsonObj);

//to add .. on all husband's name;
var name = family.find('husband').find('name');

name.appendAt(2, '..');

console.log(name.value());

// will return
["Ma..tthew", "Wi..lliam", "Ed..ward", "Ti..mothy", "Ja..son", "Wi..lliam", "Ti..mothy", "Da..niel", "Ge..orge", "Br..ian", "Fr..ank", "La..rry", "Br..ian", "Sc..ott", "Ch..arles", "Sc..ott", "Ge..orge", "Ja..mes"]

//to append a daughter in all daughter's key
var newDaughter = {
    "age": 24,
    "name": "Michelle",
    "husband": {
        "age": 30,
        "name": "Matthew"
    }
}

family.find('daughters').appendAt(1, newDaughter);

setPathValue

It set value for a specified path in JSON.
Syntax: .setPathValue(path,value)
Return: this (a jsonQ object, so it can be chained but need to be refreshed before any other method is called).
Parameters
path(array) :path is an array of key through which you access a value at any level. like ["fathers",0,"daughter",0,"name"]; value(any valid json data type) :value which you want to set at specified path.
Example

var family=jsonQ(jsonObj),
path=["fathers",0,"daughter",0,"name"]

family.setPathValue(path,"Julia");					

each

: Loop on each element of current set of matched element and apply any logic defined in callback function.
Syntax: .each(callback)
Return: this( a jsonQ object, so it can be chained ).
Parameters
callback(boolean , dafault to false); : Logic you want to apply on each value of current set. Following parameters are passed on callback function.
  1. index: index of each element
  2. path: path of each element
  3. value: value of each element
Example

var family = jsonQ(jsonObj);


//to find all father's name and age whose any of daughter's husband age is greater than or equal to 30
var husband = family.find('husband', function () {
    return this.age > = 30;
});

var father = husband.closest('daughters').parent();

father.each(function (index, path, value) {
    console.log(value.name + '  ' + value.age);
});

//will return
James Martin  44

Thomas Young  56

Thomas Gonzalez  51

Kenneth Brown  58

Thomas Lee  50                    

sort

It sort the JSON in basis of specified key and settings (options).
Syntax: .sort(key,settings)
Return: this.find(key) (a jsonQ object as selected by key provided in sort method. It can be chained but need to be refreshed before any other method is called).
Parameters
key(string) :key in which basis you want to sort a JSON.
settings(object) :settings or option to control sorting. following settings you can change for sorting.
  1. Order (ASC or DESC , dafault to ASC) : Set the order of sorting.
  2. caseIgnore (Boolean , dafault to TRUE) : If true sort by case insensitive else if false sort by case sensitive.
  3. allLevel (Boolean , dafault to TRUE) : If JSON is of multilevel array and allLevel is TRUE It will sort all level recursively else only sort those array where key is found.
  4. logic (function) : A conversion logic which convert the comparison data to some other data which will be finally compared for sorting.
    ex:
    
    function (data) {
        return data.length;
    }
    
    or
    
    function (data) {
        return data.toLowerCase();
    }
    
    
Following options also can be set globally for all instance like:
jsonQ.settings.sort.order="DESC";
or
jsonQ.settings.sort.caseIgnore=false;
Example

var family = jsonQ(jsonObj);

family.sort('name');

//if you want to sort by length of name
family.sort('name', {
    logic: function (data) {
        return data.length;
    }
});

refresh

It refresh the jsonQ object to update newly added value or changed path. After any manipulation method you must call refresh method while chaining or if you have already cached any jsonQ object.
Syntax: .refres(path,value)
Return: this (a jsonQ object, so it can be chained).
Parameters
path(array) :path is an array of key through which you access a value at any level. like ["fathers",0,"daughter",0,"name"]; value(any valid json data type) :value which you want to set at specified path.
Example

var family=jsonQ(jsonObj),
path=["fathers",0,"daughter",0,"name"]

family.setPathValue(path,"Julia");					

Return Method

value

Return value of all matched element in a jsonQ object in a array.
Syntax: .value()
Return: an array of value.
Example

var family = jsonQ(jsonObj),
    //to find all the name
    name = family.find('name'),
    value = name.value();



//to print list of all name
console.log(value);

//will return
["James Martin", "Michelle", "Matthew", "Angela", "William", "David Thompson", "Amy", "Edward", "Dorothy", "Timothy", "Thomas Young", "Sharon", "Jason", "Carol", "William", "Brenda", "Timothy", "Jason Martinez", "Jessica", "Daniel", "Thomas Gonzalez", "Brenda", "George", "Dorothy", "Brian", "James Lee", "Sarah", "Frank", "Carol", "Larry", "Kenneth Brown", "Ruth", "Brian", "Lisa", "Scott", "Sandra", "Charles", "Thomas Lee", "Patricia", "Scott", "Jennifer", "George", "Robert Anderson", "Angela", "James"]


index

Return index of spcified element in set of all matched element in a jsonQ object.
Syntax: .index(elm,isQualifier)
Return: index( number ).
isQualifier(boolean , dafault to false); : It tells weather the first parameter elm is a qualifier or not. elm(any valid json datatype object / function) : elm can be direct object for which you want to find index or it can be a qualifier so it will return the first element which passes on qualifier condition. Following senarios an elm can be used as qualifier.
  1. If elm is a function than each element of current set in jsonQ object will be passed on function to check weather it is satisfying qualifier condition or not.
  2. If current set of jsonQ object is an array of JSON and elem is key value pair. Then the index of first element of current set which have key value pair defined in qualifier will be returned.
  3. If current set of jsonQ object is an array of array and elem is array or string. Then the index of first element of current set which have all elements of qualifier will be returned.
Example

var family = jsonQ(jsonObj),
    //to find all the name
    name = family.find('name');

//to get index of Angela
var index = name.index("Angela")

//value of index will be 3  

//to get index of name which start from M
var index = name.index(function () {
    return this[0].toLowerCase() == "m";
})
//value of index will be 1 

//to return the husband whose age is 26
var index = family.find('husband').index({
    "age": 26
}, true);
//value of index will be 2


path

Return path of first element in set of all matched element in a jsonQ object.
Syntax: .path()
Return: array defining the path of element.
Example

var family = jsonQ(jsonObj),
    //to find all the name
    name = family.find('name');

console.log(name.path());

//will return
["fathers", "0", "name"]

pathValue

Return value of specified path of a JSON.
Syntax: .pathValue(path)
Return: value at specified path.
Parameters
path (array) : A path array which contains all key to reach a particular value in JSON.
Example

var family=jsonQ(jsonObj);

console.log(family.pathValue(["fathers", "0", "name"]));

//will return
"James Martin"                                   	


firstElm

Return first element value from current set of matched element.
Syntax: .firstElm()
Return: value of first element.
Example

var family=jsonQ(jsonObj),
	name=family.find('name');

console.log(name.firstElm());

//will return
"James Martin"                                   	


lastElm

Return last element value from current set of matched element.
Syntax: .lastElm()
Return: value of last element.
Example

var family=jsonQ(jsonObj),
	name=family.find('name');

console.log(name.lastElm());

//will return
"James"                                   	


nthElm

return array of elements or a single element from current set of matched element, which matches the pattern.
Syntax: .nthElm(pattern,[arrayReturn])
Return: array of elements or single element
Parameters
pattern(string / number); : A pattern can be written on following ways
  1. index (number): return element present at that index
  2. first: return first element
  3. last: return last element
  4. random: return a random element
  5. even: return all elements in a array which are on even index
  6. odd: return all elements in a array which are on odd index
  7. pattern containing n(like 2n+1 or n*n): return all elements in a array whose index follows the pattern
arrayReturn(boolean) : If true result will be always an array.
Example

var family=jsonQ(jsonObj),
	name=family.find('name');


var result=name.nthElm('even');

//value of result will be
["James Martin", "Matthew", "William", "Amy", "Dorothy", "Thomas Young", "Jason", "William", "Timothy", "Jessica", "Thomas Gonzalez", "George", "Brian", "Sarah", "Carol", "Kenneth Brown", "Brian", "Scott", "Charles", "Patricia", "Jennifer", "Robert Anderson", "James"]

var result=name.nthElm('2n*n')
//value of result will be
["James Martin", "Matthew", "Dorothy", "Jessica", "Brian"]                                	


unique

Return unique element from current set of matched element.
Syntax: .unique()
Return: an array of unique values.
Example

var family=jsonQ(jsonObj),
	name=family.find('name');

console.log(name.unique());

//will return
["James Martin", "Michelle", "Matthew", "Angela", "William", "David Thompson", "Amy", "Edward", "Dorothy", "Timothy", "Thomas Young", "Sharon", "Jason", "Carol", "Brenda", "Jason Martinez", "Jessica", "Daniel", "Thomas Gonzalez", "George", "Brian", "James Lee", "Sarah", "Frank", "Larry", "Kenneth Brown", "Ruth", "Lisa", "Scott", "Sandra", "Charles", "Thomas Lee", "Patricia", "Jennifer", "Robert Anderson", "James"]                                   	


createXML

Convert Current set of matched element in a xml format.
Syntax: .createXML()
Return: xml string.
Example

var family=jsonQ(jsonObj),
	name=family.find('name');

var familyXML= family.createXML();
var nameXML= name.createXML();

prettify

Return a pretty string of value of element of current set of matched element to be display in console or html.
Syntax: .prettify([htmlReturn])
Return: a prettified string .
Parameters
htmlReturn(boolean) : If true return the result in html form , if false return the result for console.
Example

var family=jsonQ(jsonObj),
	name=family.find('name');


var familyPr= family.prettify(true);
var namePr= name.prettify(true);

Utility Method

each

: Loop on each element of JSON or array and return it on callback function.
Syntax: jsonQ.each(json,callback)
Return: null.
Parameters
json :a JSON which you want to loop callback(boolean , dafault to false); : Logic you want to apply on each loop .
  1. key: key of current element
  2. value: value of current element
Example

var testJson = {
    "b": "some",
    "a": "someMore",
}

jsonQ.each(testJson, function (key, value) {
    console.log(key + ' : ' + value);
});

//will return
"b":"some"
"a":"someMore"


objType

: Returns object type of any data. Limited only to json data types.
Syntax: jsonQ.objType(data)
Return: string indicating object type (object,array,string,number,boolean).
Parameters
data :data for which you want its type
Example

var testJson = {
    "b": "some",
    "a": "someMore",
    "c": [1, 2]
}

jsonQ.objType(testJson);
//will return json

jsonQ.objType(testJson.b);
//will return string

jsonQ.objType(testJson.c);
//will return array

merge

: Merge the contents of two or more JSON together into the first JSON.
Syntax: jsonQ.objType([deep],target,[json1] [,jsonN])
Return: target JSON with all other objects merged on it.
Parameters
deep (boolean) : If true, the merge becomes recursive to all level (if there any other JSON in down the hierarichy of a JSON). target : Target JSON in which merge will happen. json1 - jsonN : n number of objects you want to merge on target JSON.
Example

var testJson = {
    "b": "some",
    "a": "someMore",
    "c": {
        "d": 1,
        "e": 2
    }
}
var testJson2 = {
    "c": {
        "e": 3
    },
    "f": "something"
}

jsonQ.merge(testJson, testJson2);
//will change the testJson to
{
    "b": "some",
    "a": "someMore",
    "c": {
        "e": 3
    },
    "f": "something"
}


jsonQ.merge(true, testJson, testJson2);
//will change the testJson to
{
    "b": "some",
    "a": "someMore",
    "c": {
        "d": 1,
        "e": 3
    },
    "f": "something"
}


jsonQ.merge({}, testJson, testJson2);
//will store merged JSON(testJson and testJson2) in new object

sort

It sort the array in basis of specified settings (options).
Syntax: jsonQ.sort(array,settings)
Return: sorted array .
Parameters
array(array) :Array you want to sort. It can be array of string or array of JSON. settings(object) :settings or option to control sorting. following settings you can change for sorting.
  1. Order (ASC or DESC , dafault to ASC) : Set the order of sorting.
  2. caseIgnore (Boolean , dafault to TRUE) : If true sort by case insensitive else if false sort by case sensitive.
  3. logic (function) : A conversion logic which convert the comparison data to some other data which will be finally compared for sorting.
    ex:
    
    function (data) {
        return data.length;
    }
    
    or
    
    function (data) {
        return data.toLowerCase();
    }
    
    
Following options also can be set globally for all instance like:
jsonQ.settings.sort.order="DESC";
or
jsonQ.settings.sort.caseIgnore=false;
Example

var person=[{"name":"Sudhanshu"},{"name":"Aadil"},{"name":"Mathew"}];

jsonQ.sort(person);

console.log(person);
//will give
[{"name":"Aadil"},{"name":"Mathew"},{"name":"Sudhanshu"}]



order

order a JSON (keep all key of JSON in proper series) recursively for all level .
Syntax: jsonQ.order(json)
Return: ordered JSON.
Parameters
json : JSON which you want to order.
Example

var testJson = {
    "b": "some",
    "a": "someMore",
    "1": {
        "e": "any",
        "c": "anything"
    }
}

jsonQ.order(testJson);

console.log(jsonQ.prettify(testJson));
//will return
{
    "1": {
        "c": "anything",
        "e": "any"
    },
    "a": "someMore",
    "b": "some"
}

clone

clones a JSON .
Syntax: jsonQ.clone(json)
Return: duplicate JSON.
Parameters
json : JSON which you want to clone.
Example

var testJson = {
    "b": "some",
    "a": "someMore",
    "1": {
        "e": "any",
        "c": "anything"
    }
}

var testJson2 = jsonQ.clone(testJson);

//now changing anything in testJson2 will not change testJson
testJson2.b = "somthing";

console.log(testJson2.b)
console.log(testJson.b);

//will return
something
some

index

Return index of spcified element in a array.
Syntax: jsonQ.index(array,elm,isQualifier)
Return: index( number ).
Parameters
array(array); : Array in which you want to find index of any element. isQualifier(boolean , dafault to false); : It tells weather the second parameter elm is a qualifier or not. elm(any valid json datatype object / function) : elm can be direct object for which we want to find index or it can be a qualifier so it will return the first element which passes on qualifier condition. Following senarios an elm can be used as qualifier.
  1. If elm is a function than each element of array will be passed on function to check weather it is satisfying qualifier condition or not.
  2. If array is an array of JSON and elem is key value pair. Then the index of first element of array which have key value pair defined in qualifier will be returned.
  3. If array is an array of array and elem is array or string. Then the index of first element of array which have all elements of qualifier will be returned.
Example

var tempAry = [{"name":"Alen","age":30},{"name":"Joshep","age":28},{"name":"Kristina","age":35}] ;

var index = jsonQ.index(tempAry,{"name":"Joshep","age":28});

//value of index will be 1

var index = jsonQ.index(tempAry,{"age":30},true);
//value of index will be 0

var index = jsonQ.index(tempAry, function () {
    return this.age >
        30
}, true);
//value of index will be 2;

contains

check weather an array contains a element.
Syntax: jsonQ.contains(array,elm,isQualifier)
Return: boolean (true / false).
Parameters
array(array); : Array in which you want to check that contains element or not. isQualifier(boolean , dafault to false); : It tells weather the second parameter elm is a qualifier or not. elm(any valid json datatype object / function) : elm can be direct object which you want to check an array contains or not, or it can be a qualifier so if any of element passes on qualifier condition method will return true. Following senarios an elm can be used as qualifier.
  1. If elm is a function than each element of array will be passed on function to check weather it is satisfying qualifier condition or not.
  2. If array is an array of JSON and elem is key value pair, then if any of element of array have key value pair defined in qualifier , method will return true.
  3. If array is an array of array and elem is array or string, then if any of element of array have all elements of qualifier, method will return true.
Example

var tempAry = [{"name":"Alen","age":30},{"name":"Joshep","age":28},{"name":"Kristina","age":35}] ;

var present = jsonQ.contains(tempAry,{"name":"Joshep","age":28});

//value of present will be true

var present = jsonQ.contains(tempAry,{"age":50},true);
//value of present will be false

var present = jsonQ.contains(tempAry, function () {
    return this.age >
        30
}, true);
//value of present will be true;

nthElm

return array of elements or a single element which matches the pattern.
Syntax: jsonQ.nthElm(array,pattern,[arrayReturn])
Return: array of elements or single element
Parameters
array(array); : Array from which you want to extract elements. pattern(string / number); : A pattern can be written on following ways
  1. index (number): return element present at that index
  2. first: return first element
  3. last: return last element
  4. random: return a random element
  5. even: return all elements in a array which are on even index
  6. odd: return all elements in a array which are on odd index
  7. pattern containing n(like 2n+1 or n*n): return all elements in a array whose index follows the pattern
arrayReturn(boolean) : If true result will be always an array.
Example

var tempAry = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

var result = jsonQ.nthElm(tempAry, 'even');
//value of present will be [0,2,4,6,8]

var result = jsonQ.nthElm(tempAry, '2n*n');
//value of present will be [0,2,8]

prettify

Return a pretty string of specified JSON to be display in console or html.
Syntax: .prettify(json,[htmlReturn])
Return: a prettified string .
Parameters
json(JSON) : JSON you want to pretty. htmlReturn(boolean) : If true return the result in html form , if false return the result for console.
Example

var tempAry = [{"name":"Alen","age":30},{"name":"Joshep","age":28},{"name":"Kristina","age":35}] ; 
//get prettified string to print on console
var prettified = jsonQ.prettify(tempAry) ;                 

//get prettified string to display on html
var prettified = jsonQ.prettify(tempAry,true) ;                 

identical

check two JSON are identical or not.
Syntax: jsonQ.identical(json1,json2)
Return: boolean - if identical true else false .
Parameters
json1,json2 : two JSON which you want to compare.
Example

var tempJson = {"person":[{"name":"Alen","age":30},{"name":"Joshep","age":28},{"name":"Kristina","age":35}]},
	tempJson2 = {"person":[{"name":"Alen","age":30},{"name":"Joshep","age":28},{"name":"Kristina","age":35}]}, 
	tempJson3 = {"person":[{"name":"Alen","age":30},{"name":"Joshep","age":28}]} ; 

//to compare
var result = jsonQ.identical(tempJson,tempJson2);
//value of result will be true

var result = jsonQ.identical(tempJson,tempJson3);
//value of result will be false

union

return union of two or more array in new array.
Syntax: jsonQ.union(ary1,ary2,[aryN])
Return: array - union of all array .
Parameters
ary1,ary2 .... aryN : All array as argument for which you want union.
Example

var ary1 = [1, 2, 3, 4, 5, 6],
    ary2 = [5, 6, 7, 8],
    ary3 = [2, 6, 9];

//to compare
var result = jsonQ.union(ary1, ary2, ary3);
//value of result will be [1,2,3,4,5,6,7,8,9]

intersection

return intersection of two or more array in new array.
Syntax: jsonQ.intersection(ary1,ary2,[aryN])
Return: array - intersection of all array .
Parameters
ary1,ary2 .... aryN : All array as argument for which you want intersection.
Example

var ary1 = [1, 2, 3, 4, 5, 6],
    ary2 = [2, 3, 7, 4],
    ary3 = [2, 6, 4];

var result = jsonQ.intersection(ary1, ary2, ary3);
//value of result will be [2,4]

suffle

suffle the element and return a new array of suffled elements.
Syntax: jsonQ.suffle(array)
Return: array - suffled array .
Parameters
array : Array which you want to suffle.
Example

var ary1 = [1, 2, 3, 4, 5, 6];

var result = jsonQ.suffle(array);
//value of result may be [2,4,1,6,3,5]

unique

retun unique elements of an array in new array.
Syntax: jsonQ.unique(array)
Return: array
Example

var ary1 = [1, 2, 3, 2, 5, 1];

var result = jsonQ.unique(array);
//value of result may be [1,2,3,5]

pathValue

Return value of specified path of a json.
Syntax: jsonQ.pathValue(json,path)
Return: value at specified path.
Parameters
json : JSON from which you want to get data. path (array) : A path array which contains all key to reach a particular value in JSON.
Example

var tempJson = {"person":[{"name":"Alen","age":30},{"name":"Joshep","age":28},{"name":"Kristina","age":35}]};

console.log(jsonQ.pathValue(tempJson,["person", "0", "name"]));

//will return
"Alen"                                   	

setPathValue

It set value for a specified path in JSON.
Syntax: jsonQ.setPathValue(json,path,value)
Return: json .
Parameters
json : JSON to which you want to set data. path(array) :path is an array of key through which you access a value at any level. like ["fathers",0,"daughter",0,"name"]; value(any valid json data type) :value which you want to set at specified path.
Example

var tempJson = {"person":[{"name":"Alen","age":30},{"name":"Joshep","age":28},{"name":"Kristina","age":35}]};


jsonQ.setPathValue(tempJson,["person", "0", "name"],"Daniel")

console.log(tempJson.person[0].name);
//will return
"Daniel"                                   	

createXML

Convert JSON in a xml format.
Syntax: jsonQ.createXML(json)
Return: xml string.
Parameters
json : JSON of which you want xml.
Example

var xml=jsonQ.createXML(jsonObj),

Qualifier

A qualifier can be defined in following ways.
  1. String: It can be "first" - give the first element , "last" -give the last element, "even" - give even elements, "odd" - give odd elements , "random" - give a random element
  2. Index: Index of element like 0,2 or 6
  3. Pattern: A pattern containing n(like 2n+1 or n*n) which return all elements in a array whose index follows the pattern
  4. Key value pair(json): Match key value on each element and return only those element which contain those key value pair. like {"age":24,"name":"Alen"}
  5. A Qualifier function: All element's value are passed as "this" in qualifier function and only thos element are returned which match the condition inside the function.
    
    function(){
    	return this.length>7;
    }
    
    
This documentaion is moved to http://ignitersworld.com/lab/jsonQ.html. It will be no longer maintained here .