0

Is it possible to determine if a JSON object contains a specified "fieldname".
For example: If I have a JSON Object such as:

{"event":
    [
        {"query":
            {"id":
                [
                    {"timestamp_usec":"1316596939223064"}
                ],
            "query_text":"sometext"
            }
        },
        {"query":
            {"id":
                [
                    {"timestamp_usec":"1316318681908642","type":"sometype","abc":"someabc"},
                    {"timestamp_usec":"1316318679366796"}
                ],
            "query_text":"someothertext"
            }   
        }, 
        //...More...//
    ]   
}

What can I use as my conditional within an if statement to determine if a particular "id object" contains an "abc"?

More Simply:

FOR ALL jsonpath id's
    IF jsonpath.id **CONTAINS** "abc" 
       THEN do something
    ELSE do something different
END LOOP

I'm looking for the jQuery function to achieve this (if there is one!).

My Code:

$.getJSON("test.json", function(data) {
    var search = data.event
    $.each(data.event, function(i, item) { 
        var searchMeta = data.event[i].query.id;
        $.each(searchMeta[i], function(i, deeper){
            if (searchMeta[i].  == "abc"){
               //do something 
            } else { 
               //do something different
            }
        })
    })
});

I know in the above example I could essentially achieve what I want by looping on the number of id objects, eg If num < 1. But I am not sure how uniform my data is throughout more than one .json file.

Dan
  • 448
  • 7
  • 18

2 Answers2

0

JSON.stringify(json) this will give you json as string then you can do REGEX to check if there is file in the json.

Alaa Abuzaghleh
  • 1,023
  • 6
  • 11
0

Try defining a function to call for each Object within data object

   // property `name` to check
    var name = "abc";

    var check = function check(obj, name) {
        // if `obj` is `Object` ,
        // and `obj` has property `name` ,
        // do stuff
        if (/Object/.test(obj.constructor.toString()) 
           && obj.hasOwnProperty(name)) {
          console.log(name + " found")
        } 
        // else , do other stuff
        else {
          console.log(name + " not found")
        }
    };

    $.each(data.event, function(key, value) {
      check(value, name);
      $.each(value.query, function(k, v) {
        check(v, name);
          // if `v` is an `Object` , call check for 
          // call `check` on each `v` `Object` 
          if (typeof v === "object") {
            $.each(v, function(_k, _v) {
              check(_v, name);
            })
          };
      });
    });

var data = {
  "event": [{
      "query": {
        "id": [{
          "timestamp_usec": "1316596939223064"
        }],
        "query_text": "sometext"
      }
    }, {
      "query": {
        "id": [{
          "timestamp_usec": "1316318681908642",
          "type": "sometype",
          "abc": "someabc"
        }, {
          "timestamp_usec": "1316318679366796"
        }],
        "query_text": "someothertext"
      }
    }
    //...More...//
  ]
};

var data = {
  "event": [{
      "query": {
        "id": [{
          "timestamp_usec": "1316596939223064"
        }],
        "query_text": "sometext"
      }
    }, {
      "query": {
        "id": [{
          "timestamp_usec": "1316318681908642",
          "type": "sometype",
          "abc": "someabc"
        }, {
          "timestamp_usec": "1316318679366796"
        }],
        "query_text": "someothertext"
      }
    }
    //...More...//
  ]
};


// property `name` to check
var name = "abc";

var check = function check(obj, name) {
  // if `obj` is `Object` ,
  // and `obj` has property `name` ,
  // do stuff
  if (/Object/.test(obj.constructor.toString()) 
      && obj.hasOwnProperty(name)) {
    var res = {};
    res[name] = obj[name];
    $("body").append(JSON.stringify(res) + " found")
  }
  // else , do other stuff
  else {
    console.log(name + " not found")
  }
};

$.each(data.event, function(key, value) {
  check(value, name);
  $.each(value.query, function(k, v) {
    check(v, name);
    // if `v` is an `Object` , call check for 
    // call `check` on each `v` `Object` 
    if (typeof v === "object") {
      $.each(v, function(_k, _v) {
        check(_v, name);
      })
    };
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

jsfiddle http://jsfiddle.net/yenp6t8v/

guest271314
  • 1
  • 15
  • 104
  • 177