65.9K
CodeProject is changing. Read more.
Home

Selecting JSON Objects

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Mar 23, 2011

CPOL

2 min read

viewsIcon

24936

Often while working with the web services that return JSON objects to the client side I find a need to select some particular JSON objects in the client side based upon certain criteria.

Introduction

This article describes how to select a particular object from the array of JSON objects.

Background

Often while working with the web services that return JSON objects to the client side I find a need to select some particular JSON objects in the client side based upon certain criteria.

The straight forward way is to loop through the JSON objects and select the particular object based upon the criteria. So what I thought of is that can I make a reusable code so that it can help others in selecting a particular JSON object based upon a certain criteria.

Using the Code

Let us take the following JSON string.
[{ "TEST1": 45, "TEST2": 23, "TEST3": "DATA1" }, { "TEST1": 46, "TEST2": 24, 
    "TEST3": "DATA1" }, { "TEST1": 47, "TEST2": 25, "TEST3": "DATA3"}];
Now we want to select the JSON objects based upon the following criteria.
TEST1 = 45 OR TEST3 = ‘DATA1’
The following script can help in selecting the JSON object. Use any of these “sql” function based upon your need which can serve your purpose.

Code Snippet for Selecting JSON Object

function sql(s) {
     var returnObj = new Array();
     var cntr = 0;
     var cnt;
     for (var bb = 0; bb < s.from.length; bb++)
     {
     //$.each(s.from, function(bb) {
         var ifConditions = new Array();
         for (cnt = 0; cnt < s.where.length; cnt++) {
             ifConditions[cnt] = new Object();
             var where = "";
             if (s.where[cnt].OPERATOR.indexOf("=") == 0)
                 where = "==";
             if (s.where[cnt].VALUE.indexOf("'") > -1)
                 ifConditions[cnt] = eval("'" + eval("s.from[bb]." + (eval("s.where[" + cnt + "].KEY"))) + "'" + where + eval("s.where[" + cnt + "].VALUE"));
             else
                 ifConditions[cnt] = eval(eval("s.from[bb]." + (eval("s.where[" + cnt + "].KEY"))) + where + eval("s.where[" + cnt + "].VALUE"));
         }
         var comparedOutput = true;
         for (cnt = 0; cnt < s.conditions.length; cnt++) {
             var condition = "";
             switch (s.conditions[cnt].CONDITION.toUpperCase()) {
                 case "AND":
                     condition = "&&";
                     break;
                 case "OR":
                     condition = "||";
                     break;
             }
             comparedOutput = comparedOutput && eval("ifConditions[" + s.conditions[cnt].Condition1 + "]" + condition + "ifConditions[" + s.conditions[cnt].Condition2 + "]");
         }
         if (comparedOutput) {
             var result = {};
             var cols = s.select.split(",");
             for (var cnt = 0; cnt < cols.length; cnt++) {
                 result[cols[cnt]] = eval("s.from[bb]." + cols[cnt]);
             }
             returnObj.push(result);
         }
     }
     return returnObj;
 }

Code Snippet for Selecting JSON Object using jQuery

function sql(s) {
        var returnObj = new Array();
        var cntr = 0;
        $.each(s.from, function(bb) {
            var ifConditions = new Array();
            $.each(s.where, function(cnt) {
                ifConditions[cnt] = new Object();
                var where = "";
                if (s.where[cnt].OPERATOR.indexOf("=") == 0)
                    where = "==";
                if (s.where[cnt].VALUE.indexOf("'") > -1)
                    ifConditions[cnt] = eval("'" + eval("s.from[bb]." + (eval("s.where[" + cnt + "].KEY"))) + "'" + where + eval("s.where[" + cnt + "].VALUE"));
                else
                    ifConditions[cnt] = eval(eval("s.from[bb]." + (eval("s.where[" + cnt + "].KEY"))) + where + eval("s.where[" + cnt + "].VALUE"));
            });
            var comparedOutput = true;
            $.each(s.conditions, function(cnt) {
                var condition = "";
                switch (s.conditions[cnt].CONDITION.toUpperCase()) {
                    case "AND":
                        condition = "&&";
                        break;
                    case "OR":
                        condition = "||";
                        break;
                }
                comparedOutput = comparedOutput && eval("ifConditions[" + s.conditions[cnt].Condition1 + "]" + condition + "ifConditions[" + s.conditions[cnt].Condition2 + "]");
            });
            if (comparedOutput) {
                var result = {};
                var cols = s.select.split(",");
                for (var cnt = 0; cnt < cols.length; cnt++) {
                    result[cols[cnt]] = eval("s.from[bb]." + cols[cnt]);
                }
                returnObj.push(result);
            }
        });
        return returnObj;
    }
How to use these above functions to select those JSON objects whose values for TEST1 = 45 OR TEST3 = ‘DATA1’
var selectedObjs = sql({
                select: "TEST1,TEST2",
                from: a,
                where: [{ "KEY": "TEST1", "OPERATOR": "=", "VALUE": "45" }, { "KEY": "TEST3", "OPERATOR": "=", "VALUE": "'DATA1'"}],
                conditions: [{ "Condition1": "0", "CONDITION": "Or", "Condition2": "1"}]
            });
In the above highlighted code snippet we are passing the JSON object, containing the criteria for selection of JSON objects, to pass to the sql javascript function.

The JSON object contains the following keys

  1. select: Mention the keys/properties you want to select out of the JSON object.
  2. from: Mention the JSON object from which the selection will be made.
  3. where: Mention the conditions/selection criterias in JSON format.
  4. conditions: Here AND/OR conditions between different where clauses can be specified. Here “Condition1”:“0” signifies the index of first where clause mentioned ({ "KEY": "TEST1", "OPERATOR": "=", "VALUE": "45" }). Similarly “Condition1”:”1” signifies ({ "KEY": "TEST3", "OPERATOR": "=", "VALUE": "’DATA1’" }). The “CONDITION”:”OR” specifies the OR clause.
Note that this is a small approach and this doesn’t address complex conditions. One option is that you can take care of the complex condition in the server side itself.

Points of Interest

This is just a try to get the feel of Language Integrated Query (LINQ) or SQL like statement on JSON objects in client side.