How to filter the json string

Bharat Guru
Bharat Guru used Ask the Experts™
on
How to write a generic function in jquery or javascript where I can filter the data as below
     JsonFruit_US =  filter the  Json_Data 'Location=US' and RecordType='Fruits'
     JsonGrocery_US_US =  filter the  Json_Data 'Location=US' and RecordType='Grocery'
     JsonUS =  filter the  Json_Data 'Location=US'

String Json_Data = 
{
  "action": "query",
  "Warehouse": "NJ",
  "records": [
      {
		"Location": "US"'
		"RecordType": "Fruits"'
        "Productid": 123,
        "Qtry": 5,
        "Name": "Apple",
      },
      {
		"Location": "UK"'
		"RecordType": "Fruits"'
        "Productid": 124,
        "Qtry": 11,
        "Name": "Bananna",
      }
      {
		"Location": "US"'
		"RecordType": "Grocery"'
        "Productid": 223,
        "Qtry": 5,
        "Name": "Rice-UK",
      },
      {
		"Location": "UK"'
		"RecordType": "Grocery"'
        "Productid": 224,
        "Qtry": 11,
        "Name": "Beans-US",
      }
    ]
};

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Use Jquery grep on your array as shown below:
var data = 
{
  "action": "query",
  "Warehouse": "NJ",
  "records": [
      {
		"Location": "US"'
		"RecordType": "Fruits"'
        "Productid": 123,
        "Qtry": 5,
        "Name": "Apple",
      },
      {
		"Location": "UK"'
		"RecordType": "Fruits"'
        "Productid": 124,
        "Qtry": 11,
        "Name": "Bananna",
      }
      {
		"Location": "US"'
		"RecordType": "Grocery"'
        "Productid": 223,
        "Qtry": 5,
        "Name": "Rice-UK",
      },
      {
		"Location": "UK"'
		"RecordType": "Grocery"'
        "Productid": 224,
        "Qtry": 11,
        "Name": "Beans-US",
      }
    ]
};
var JsonUS = $.grep(data.items, function (element, index) {
    return element.Location == 'US';
});
var JsonFruit_US= $.grep(data.items, function (element, index) {
    return element.Location == 'US' && element.RecordType='Fruits';
});
var JsonGrocery_US= $.grep(data.items, function (element, index) {
    return element.Location == 'US' && element.RecordType='Grocery';
});

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Firstup your JSON is invalid you have extra commas, missing commas and ' instead of commas - here is the corrected string
{
   "action":"query",
   "Warehouse":"NJ",
   "records":[
      {
         "Location":"US",
         "RecordType":"Fruits",
         "Productid":123,
         "Qtry":5,
         "Name":"Apple"
      },
      {
         "Location":"UK",
         "RecordType":"Fruits",
         "Productid":124,
         "Qtry":11,
         "Name":"Bananna"
      },
      {
         "Location":"US",
         "RecordType":"Grocery",
         "Productid":223,
         "Qtry":5,
         "Name":"Rice-UK"
      },
      {
         "Location":"UK",
         "RecordType":"Grocery",
         "Productid":224,
         "Qtry":11,
         "Name":"Beans-US"
      }
   ]
}

Open in new window

Secondly you don't need to use grep for this - there is a JavaScript function for filtering array records called filter()

Assuming you have parsed your JSON return into a variable called Json_data you would use it like this

var  JsonFruit_US = Json_Data.records.filter(function(item) { 
  return item.Location == 'US' && item.RecordType == 'Fruits';
});
var  JsonGrocery_US_US = Json_Data.records.filter(function(item) {
  return item.Location == 'US' && item.RecordType == 'Grocery';
});
var JsonUS = Json_Data.records.filter(function(item) {
  return item.Location == 'US';
});

Open in new window


NB If you want to use the $.grep solution then you will need to fix the code above
1. It has an assignment operator (=) in place of an equality operator (==) on the second test
2. It refers to data.items instead of data.records
Corrected code
var res = $.grep(data.records, function (element, index) {
  return element.Location == 'US' && element.RecordType=='Fruits';
});

Open in new window

Author

Commented:
Works great

Author

Commented:
how can I filter if my element name has "/" like "Location/PRODUCT"

var res = $.grep(data.records, function (element, index) {
  return element."Location/PRODUCT" == 'US' ;
});
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
@Bharat Guru
You seem to be using the $.grep solution posted by Miguel but you accepted the solution using filter?
If you are using $.grep then you should be giving credit to Miguel.

For the filter solution you could do this - similar for the grep
return item.Location.split('/')[0] == 'US' && item.RecordType == 'Fruits';

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial