How to filter the json string

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

Bharat GuruAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
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

0
Julian HansenCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bharat GuruAuthor Commented:
Works great
0
Bharat GuruAuthor 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' ;
});
0
Julian HansenCommented:
@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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.