-1

I have to iterate through "price_detail" json array object. Please teach me how to reinerate this type of object. I have looped through many structures but not this type. I could not find an example for this type of data structure searching for "Iterate through nested json object array"

Code I use:

     $.each(json.data['price_detail'], function (i, item) { 
      console.log('name='+ i + ' value=' +item);
      }

  output:
  name=price value=14.7,14.7,14.7
  name=type value=coupon,coupon,coupon
  name=savings value=,75%,35%
  name=pharmacy value=Walmart,Kmart,Costco

Data has following structure.

{"errors": [], 
            "data": {"form": "tablet", 
            "price_detail": 
                {"price": [14.7, 14.7, 14.7], 
                "type": ["coupon", "coupon", "coupon"], 
                "savings": [null, "75%", "35%"], 
                "pharmacy": ["Walmart", "Kmart", "Costco"]},                    
                "brand": ["lortab", "maxidone", "vicodin", "norco", "xodol", "hycet"], 
                "dosage": "5mg/325mg", "generic": ["hydrocodone/acetaminophen", "lorcet", "zolvit"], 
                "prices": [14.7, 14.7, 14.7], 
                "quantity": 60, 
                "display": "Lortab, Maxidone, Vicodin, Norco, Xodol, Hycet (hydrocodone / acetaminophen, lorcet, zolvit)", 
                "manufacturer": "generic"}, 
                "success": true}

I would like an object using pharmacy, type, saving and prices; such as:

       {
            ["Walmart", "coupon", NULL, 14.7], 
            ["Kmart", "coupon", "75%", 14.7], 
            ["Costco","coupon", "35%", 14.7]
        }

Thank you.

EDIT: I changed the title. After scouring the web, I discovered I need to transpose the json result. Pardon my lack of terminology.

I attempted to use this function I found to transpose an array

    function transpose(a) {
        return Object.keys(a[0]).map(function (c) {
            return a.map(function (r) {
                return r[c];
            });
        });
    }

created test data, run it through function

    var testData = [   [14.7, 14.7, 14.7], 
                       ["coupon", "coupon", "coupon"], 
                       [null, "75%", "35%"], 
                       ["Walmart", "Kmart", "Costco"] 
                ]
                        var result = transpose(testData);

This gives me the desired results.

    [14.7, "coupon", null, "Walmart"],
    [14.7, "coupon", "75%", "Kmart"],
    [14.7, "coupon", "35%", "Costco"]

Now when I run my 'price_detail' object through it

    transpose(json.data['price_detail']);

I get stuck at this error: Uncaught TypeError: Cannot convert undefined or null to object

bradley32110
  • 53
  • 1
  • 9
  • https://stackoverflow.com/questions/19323699/iterating-through-json-object-javascript Do some research before posting, then write some code to try and solve your own problem. If you can't make it work, post your code here. This isn't a free coding service, and certainly not a free research service. You think no-one has ever tried to loop through an object before, or make a new object from the results? People will gladly spend their time and effort to help you fix your code if it can be seen that you made some effort of your own first. – ADyson Sep 26 '17 at 15:07
  • Possible duplicate of [iterating through json object javascript](https://stackoverflow.com/questions/19323699/iterating-through-json-object-javascript) P.S. I'm assuming you want to do this in javascript, although you haven't actually even tagged a language in the question. – ADyson Sep 26 '17 at 15:09
  • I did do some research, stop assuming. I did write code, thanks for asking. I do not understand how to line up the data with the columns, i believe i need to loop through the columns – bradley32110 Sep 26 '17 at 16:06
  • If you did something, you should show it here, and explain what problem you're having. We cannot read your mind, only your question. We only know what you tell us. Like I said, people want to _see_ that you made an effort - there are plenty of questions on here where people just hope that others will do their work for them, for free, maybe so they can go to the beach instead or something. It's not that hard to take another example (like the link I gave) and make your own version. You say you have done that, therefore please show your attempt here.You can use the Edit button under your question – ADyson Sep 26 '17 at 16:14

1 Answers1

-1

Your stated desired output isn't valid JSON, but here's an example which produces a format that I think may be useful to you. If it's not exactly what you'd like, you may now be able to see how to modify it to suit you.

var json = {
  "errors": [],
  "data": {
    "form": "tablet",
    "price_detail": {
      "price": [14.7, 14.7, 14.7],
      "type": ["coupon", "coupon", "coupon"],
      "savings": [null, "75%", "35%"],
      "pharmacy": ["Walmart", "Kmart", "Costco"]
    },
    "brand": ["lortab", "maxidone", "vicodin", "norco", "xodol", "hycet"],
    "dosage": "5mg/325mg",
    "generic": ["hydrocodone/acetaminophen", "lorcet", "zolvit"],
    "prices": [14.7, 14.7, 14.7],
    "quantity": 60,
    "display": "Lortab, Maxidone, Vicodin, Norco, Xodol, Hycet (hydrocodone / acetaminophen, lorcet, zolvit)",
    "manufacturer": "generic"
  },
  "success": true
};

var result = [];

for (var i = 0; i < json.data.price_detail.pharmacy.length; i++) {
  result.push({
    "pharmacy": json.data.price_detail.pharmacy[i],
    "type": json.data.price_detail.type[i],
    "savings": json.data.price_detail.savings[i],
    "price": json.data.price_detail.price[i]
  });
}
console.log(JSON.stringify(result));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Your solution worked and cleaner than what I was able to came up with. I may have edited my post before you replied with an answer. I kind of thought .push method would be useful here but I really didnt know how to transpose. – bradley32110 Sep 26 '17 at 18:58
  • No problem. Yes it looks like you edited your question in the meantime. Are you still having a problem? Do you need some help with it, or did you fix it? I can potentially modify the answer somewhat to get to your desired output format, if you like. And why the downvote, or was that not you? – ADyson Sep 26 '17 at 23:04