1

So I have the following JSON that is being displayed on an order confirmation page:

var orderDetails = {
    "orderID": "o32770183",
    "orderQty": 3,
    "orderTotal": 575.97,
    "orderShipping": 49.97,
    "orderDiscount": 0,
    "orderTax": 39.74,
    "orderCity": "Norwalk",
    "orderState": "Connecticut",
    "itemId": [
        "sku500134",
        "sku230312",
        "sku133846"
    ],
    "itemQty": [
        1,
        1,
        1
    ],
    "itemPrice": [
        159.99,
        225.99,
        189.99
    ],
    "itemName": [
        "The 'Inaugural' Raymond R Cabernet Sauvignon",
        "H de l'Hospitalet",
        "Chateau Florie Aude"
    ]
}

What would be the best approach to pulling the data out?

8
  • 2
    "So I have the following JSON..." That's not JSON. It's a JavaScript object initializer (aka "object literal"). JSON is a subset thereof, but when you have such a thing in your JavaScript code, it's not JSON. Commented Jun 6, 2012 at 20:46
  • @T.J.Crowder - cool seeing you around here again :) Commented Jun 6, 2012 at 20:49
  • @T.J.Crowder: Is there such a thing as a standard "wrapper"? JSON could really use one, if only for the purpose of changing the name. Commented Jun 6, 2012 at 20:50
  • @amnotiam: Huh? Not following you. Why would you want a wrapper around an object initializer? Commented Jun 6, 2012 at 20:53
  • @T.J.Crowder: No, not around an object, around the JSON standard so we can call it something else that doesn't start with "JS". I know, it was a stretch. ;) Commented Jun 6, 2012 at 20:57

4 Answers 4

3

Or also, like this - a more dynamic approach - (if you dont know the objects elements)

for(i in orderDetails)
  alert(orderDetails[i])
Sign up to request clarification or add additional context in comments.

Comments

2

That's actually not JSON, that's a plain old JavaScript object. You pull the data our just like with any other object

var orderId = orderDetails.orderID;

or

var orderId = orderDetails["orderID"];

or for arrays:

var itemQtyArr = orderDetails.itemQty;
for(var i = 0, max = itemQtyArr.length; i < max; i++){
   console.log("itemQty", i, itemQtyArr[i]);
}

or the dynamic approach Vivek posted (+1 to him)

8 Comments

Or as this is tagged jquery, there's always $.each(orderDetails.itemQty, function(index, entry) { ... });
Tj which is slower, i'd rather use for in
@Interstellar_Coder: Not in the real world, unless you're dealing with millions of entries: blog.niftysnippets.org/2012/02/foreach-and-runtime-cost.html
@T.J.Crowder - I accept either are equally fast (never doubted it) - but is there an advantage to using $.each over a simple for loop? Is it just personal preference?
@AdamRackis: I'd call it personal preference. With the function, you gain variable scoping and such. But totally style, I'd say. I used to do for loops. Once I tested the runtime cost, I mostly switched over to forEach and $.each for the scoping.
|
1

You can iterate the keys of a Javascript Object using for in

Lets say

a = {"a":"hello","b":"world"};

for(var c in a){
  console.log(c); //will out put a,b in iterations
  console.log(a[c]) //will access values of keys a and b from the object a output hello, world
}

3 Comments

There is no "JSON object" in your code. See comment on the question for details.
@T.J.Crowder - what would be an example of a JSON object?
@jrutter: See the comment on the question for the distinction between a JavaScript object initializer (which is what Abid is using) and JSON.
0

Here is a helpful JSON Editor tool that gives you a visual representation of your JSON object. It lists your named items and gives you the path to access your JSON values: JSON Editor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.