2

How to get a Unique merchant name and for that corresponding avgprice and numproducts using jquery? I`ve been having hard time seperating the json data. Please help me guys.. I am parsing this json file using $.getJSON. We are creating graph where we need to use seperator after every merchant names. So here avgprice and numProducts are the x and y axes values. So if I use something like this

$.getJSON("mock/Insight72.json", function(returnedData) {
        dataLength = returnedData.data.length;
console.log(returnedData)
response = returnedData;
        var x = new Array();
        var y = new Array();
        var mytext, f;

        for ( i = 0; i < dataLength; i++) {

            x[i] = returnedData.data[i].avgPrice;
            y[i] = returnedData.data[i].numProducts;

x and y axis getting stored in the same order. But I need to parse like it should take the avgprice and numProducts corresponding to the Merchant name. Hope this clarifies..

 {
    "id" : "72",
    "title" : "Item Category Product Level Average Price Comparison",
    "xLabel" : null,
    "yLabel" : "Average Price",
    "zLabel" : null,

    "data" : [
        {
            "avgPrice" : 10,
            "categoryID" : "152b7934dd7e4fbeac56f825b5deaebd",
            "categoryName" : "Coffee Makers",
            "merchantID" : "99a8cd5687d245f8bff2152fec710973",
            "merchantName" : "Crate & Barrel",
            "numProducts" : 400
        },
        {
            "avgPrice" : 20,
            "categoryID" : "152b7934dd7e4fbeac56f825b5deaebd",
            "categoryName" : "Coffee Makers",
            "merchantID" : "f5b2c736eace488a859fb6c56f366522",
            "merchantName" : "Williams-Sonoma",
            "numProducts" : 500 
        },
        {
            "avgPrice" :30,
            "categoryID" : "152b7934dd7e4fbeac56f825b5deaebd",
            "categoryName" : "Coffee Makers",
            "merchantID" : "6ee163f4f236466289fae97bb40351b7",
            "merchantName" : "Amazon",
            "numProducts" : 38
        },
        {
            "avgPrice" : 40,
            "categoryID" : "50215adc9ef64f91b4f4898fda7cf0b5",
            "categoryName" : "Dishwashers",
            "merchantID" : "99a8cd5687d245f8bff2152fec710973",
            "merchantName" : "Crate & Barrel",
            "numProducts" : 300 
        },
        {
            "avgPrice" : 50,
            "categoryID" : "50215adc9ef64f91b4f4898fda7cf0b5",
            "categoryName" : "Dishwashers",
            "merchantID" : "f5b2c736eace488a859fb6c56f366522",
            "merchantName" : "Williams-Sonoma",
            "numProducts" : 320
        },
        {
            "avgPrice" : 60,
            "categoryID" : "50215adc9ef64f91b4f4898fda7cf0b5",
            "categoryName" : "Dishwashers",
            "merchantID" : "6ee163f4f236466289fae97bb40351b7",
            "merchantName" : "Amazon",
            "numProducts" : 350
        }
    ]
}
Ash
  • 239
  • 2
  • 9
  • 25
  • need only 1 merchant name or all distinct value of merchant name? – xkeshav May 10 '13 at 12:53
  • I'm really not sure what you're asking for. There aren't any unique merchant names, they're all duplicated. What kind of input are you going to be supplying - just a merchant name, a merchant name and a category, something else? Also provide an example of the input and the corresponding output you'd expect from it. – Anthony Grist May 10 '13 at 12:57
  • Need Unique Merchant names, In this case 3.(for that avgprice and numproducts). – Ash May 10 '13 at 12:58
  • Check out either the lodash or underscore libraries. They have some great functions for pulling data. – lucuma May 10 '13 at 13:02
  • What do you mean by **for that avgprice and numproducts**? Give us an example of how the data might look once returned correctly. – LeonardChallis May 10 '13 at 13:02
  • Sorry. Please check the edited question. – Ash May 11 '13 at 03:36

2 Answers2

2

Here's the working fiddle: http://jsfiddle.net/Esv6f/

/* get all items by merchant*/
function get_items_by_merchant(merchant_name) {
    var items = new Array();
    $.each(json.data, function(index, item) {
        if (item.merchantName == merchant_name) 
            items.push(item);
    });

    return items;
}

/* get items of merchant "Amazon" */
var amazon_items = get_items_by_merchant("Amazon");

/* count the total num of products */ 
var amazon_num_products_total = 0;

/* loop items */
$.each(amazon_items, function(index, item) {
    amazon_num_products_total += item.numProducts; // add numProducts 

    /* alert the avgPrice and numProducts of the CURRENT item */
    alert("avgPrice: " + item.avgPrice + " numProducts: " + item.numProducts);
});

/* alert the total num of products of amazon */ 
alert("Amazon numProducts total: " + amazon_num_products_total);
Mr. B.
  • 8,041
  • 14
  • 67
  • 117
  • Thanks a lot for the reply. This we cannot handle when multiple data comes no? So, working on the same kinda method. – Ash May 11 '13 at 03:25
  • @Ash You're welcome! I didn't understand your question. What kind of "multiple data" do you want to handle - and how? :-) – Mr. B. May 11 '13 at 16:29
  • Yes I have to be bit more clear.. Please check this link.. http://stackoverflow.com/questions/16504621/accessing-data-in-the-json-file – Ash May 12 '13 at 05:59
0

This function will return the first match it finds:

function getMerchantName(data, avgPrice, numProducts) {
    for(x in a.data) {
        if(a.data[x].avgPrice === avgPrice && a.data[x].numProducts === numProducts)
            return a.data[x].merchantName;
    }
}

Fiddle

jterry
  • 6,209
  • 2
  • 30
  • 36