How to use variable to get data from JSON array

How can I use variable in script to get JSON data?

function GetItems(o,i,myVar) {
  var r = JSON.parse(Data.Get('Glf_Json'));  
  if (type == "count") {
    return r.orders[o].items.length;
  } else {
    return r.orders[o].items[i].myVar
  }
}

I want to pass myVar to return r.orders[o].items[i].myVar

Struggling to understand what your trying to do, can you show a sample array and sample call and what you want it to return?

It was Gloria Food JSON.

I don’t want to do Switch/Case to get the data I want. It would be simpler if I can pass Variable.

For Ex.:
r.orders[o].items[i].id
r.orders[o].items[i].name
r.orders[o].items[i].price

I would I want to use function GetItems('0','1','id') to get r.orders[o].items[i].id

Have you gont a sample of actual array?

r.orders[o].items[i].id
r.orders[o].items[i].name
r.orders[o].items[i].price

Doesnt seem right to me…

{
    "count": 1,
    "orders": [
        {
            "coupons": [],
            "id": 776113,
            "restaurant_id": 4172,
            "client_id": 188995,
            "type": "delivery",
            "sub_total_price": 47.88,
            "tax_value": 4.13,
            "total_price": 62.41,
            "client_first_name": "Donald",
            "client_last_name": "Draper",
            "client_email": "don.draper@gloriafood.com",
            "client_phone": "+15551234567",
            "restaurant_name": "John's Excellent Pizza",
            "instructions": null,
            "currency": "USD",
            "latitude": "37.79448725889753",
            "longitude": "-122.395311680426",
            "tax_type": "NET",
            "tax_name": "Sales Tax",
            "fulfill_at": "2016-02-03T17:19:32.000Z",
            "pos_system_id": 1,
            "restaurant_key": "8yCPCvb3dDo1k",
            "api_version": 2,
            "payment": "ONLINE",
            "client_address": "21 Market Street, San Francisco",
            "items": [
                {
                    "id": 1678316,
                    "name": "DELIVERY_FEE",
                    "total_item_price": 5,
                    "price": 5,
                    "quantity": 1,
                    "instructions": null,
                    "type_id": null,
                    "type": "delivery_fee",
                    "tax_rate": 0.1,
                    "tax_value": 0.5,
                    "parent_id": null,
                    "cart_discount_rate": 0,
                    "cart_discount": 0,
                    "tax_type": "NET",
                    "item_discount": 0,
                    "options": []
                },
                {
                    "id": 1678317,
                    "name": "TIP",
                    "total_item_price": 5.67,
                    "price": 5.67,
                    "quantity": 1,
                    "instructions": null,
                    "type_id": null,
                    "type": "tip",
                    "tax_rate": 0.05,
                    "tax_value": 0.2702,
                    "parent_id": null,
                    "cart_discount_rate": 0,
                    "cart_discount": 0,
                    "tax_type": "GROSS",
                    "item_discount": 0,
                    "options": []
                },
                {
                    "id": 1678322,
                    "name": "Pizza Margherita",
                    "total_item_price": 8.2,
                    "price": 7,
                    "quantity": 1,
                    "instructions": "",
                    "type_id": 58424,
                    "type": "item",
                    "tax_rate": 0.07,
                    "tax_value": 0,
                    "parent_id": 1678332,
                    "cart_discount_rate": 0,
                    "cart_discount": 0,
                    "tax_type": "NET",
                    "item_discount": 8.2,
                    "options": [
                        {
                            "id": 1771325,
                            "name": "Small",
                            "price": 0,
                            "group_name": "Size",
                            "quantity": 1,
                            "type": "size"
                        },
                        {
                            "id": 1771326,
                            "name": "Crispy",
                            "price": 0,
                            "group_name": "Crust",
                            "quantity": 1,
                            "type": "option"
                        },
                        {
                            "id": 1771327,
                            "name": "Extra mozzarella",
                            "price": 1.2,
                            "group_name": "Extra Toppings (Small)",
                            "quantity": 1,
                            "type": "option"
                        }
                    ]
                },
                {
                    "id": 1678324,
                    "name": "Pizza Prosciutto",
                    "total_item_price": 11.7,
                    "price": 8,
                    "quantity": 1,
                    "instructions": "",
                    "type_id": 58425,
                    "type": "item",
                    "tax_rate": 0.07,
                    "tax_value": 0.819,
                    "parent_id": 1678332,
                    "cart_discount_rate": 0,
                    "cart_discount": 0,
                    "tax_type": "NET",
                    "item_discount": 0,
                    "options": [
                        {
                            "id": 1771331,
                            "name": "Large",
                            "price": 2,
                            "group_name": "Size",
                            "quantity": 1,
                            "type": "size"
                        },
                        {
                            "id": 1771332,
                            "name": "Crispy",
                            "price": 0,
                            "group_name": "Crust",
                            "quantity": 1,
                            "type": "option"
                        },
                        {
                            "id": 1771333,
                            "name": "Extra mozzarella",
                            "price": 1.7,
                            "group_name": "Extra Toppings (Large)",
                            "quantity": 1,
                            "type": "option"
                        }
                    ]
                },
                {
                    "id": 1678331,
                    "name": "Pizza Prosciutto",
                    "total_item_price": 8.7,
                    "price": 8,
                    "quantity": 1,
                    "instructions": "",
                    "type_id": 58425,
                    "type": "item",
                    "tax_rate": 0.07,
                    "tax_value": 0.609,
                    "parent_id": 1678332,
                    "cart_discount_rate": 0,
                    "cart_discount": 0,
                    "tax_type": "NET",
                    "item_discount": 0,
                    "options": [
                        {
                            "id": 1771343,
                            "name": "Small",
                            "price": 0,
                            "group_name": "Size",
                            "quantity": 1,
                            "type": "size"
                        },
                        {
                            "id": 1771344,
                            "name": "Fluffy",
                            "price": 0,
                            "group_name": "Crust",
                            "quantity": 1,
                            "type": "option"
                        },
                        {
                            "id": 1771345,
                            "name": "Corn",
                            "price": 0.7,
                            "group_name": "Extra Toppings (Small)",
                            "quantity": 1,
                            "type": "option"
                        }
                    ]
                },
                {
                    "id": 1678332,
                    "name": "2 + 1 Pizza Special",
                    "total_item_price": 28.6,
                    "price": 0,
                    "quantity": 1,
                    "instructions": null,
                    "type_id": 251,
                    "type": "promo_item",
                    "tax_rate": 0.07,
                    "tax_value": 1.3566,
                    "parent_id": null,
                    "cart_discount_rate": 0.05,
                    "cart_discount": 1.02,
                    "tax_type": "NET",
                    "item_discount": 8.2,
                    "options": []
                },
                {
                    "id": 1678334,
                    "name": "Spaghetti Bolognese",
                    "total_item_price": 18,
                    "price": 9,
                    "quantity": 2,
                    "instructions": "",
                    "type_id": 58426,
                    "type": "item",
                    "tax_rate": 0.07,
                    "tax_value": 1.197,
                    "parent_id": null,
                    "cart_discount_rate": 0.05,
                    "cart_discount": 0.9,
                    "tax_type": "NET",
                    "item_discount": 0,
                    "options": []
                },
                {
                    "id": 1678335,
                    "name": "Spaghetti Frutti di Mare",
                    "total_item_price": 12,
                    "price": 12,
                    "quantity": 1,
                    "instructions": "",
                    "type_id": 58427,
                    "type": "item",
                    "tax_rate": 0.07,
                    "tax_value": 0.798,
                    "parent_id": null,
                    "cart_discount_rate": 0.05,
                    "cart_discount": 0.6,
                    "tax_type": "NET",
                    "item_discount": 0,
                    "options": []
                },
                {
                    "id": 1678336,
                    "name": "5% off total larger than 40$",
                    "total_item_price": 0,
                    "price": 0,
                    "quantity": 1,
                    "instructions": null,
                    "type_id": 250,
                    "type": "promo_cart",
                    "tax_rate": 0.07,
                    "tax_value": 0,
                    "parent_id": null,
                    "cart_discount_rate": 0.05,
                    "cart_discount": -2.52,
                    "tax_type": "NET",
                    "item_discount": 2.52,
                    "options": []
                }
            ]
        }
    ]
}
1 Like

Ok, so you want one script to pull a specified input array value from o/i index item?
Think this came up in my PMS script at one point and think its tricky to do…

PMS thread is way to big to go thru. Do you have a exact location :wink:

There must be a better way to get specific element.

It has so many elements. It is going to be long Switch/Case, copy paste:(

:slight_smile: it came up in a question not as a solution.

However pretty sure I have your solution :wink: give me couple of minutes

OK, got it I think :slight_smile:

eval()

var parsedArray = JSON.parse(array)
return eval('parsedArray.orders[0].'+field)
}

eval() looks like it allows you to do array lookup based on string which means we can use + to make the array index string. So;

return eval('r.orders['+o+'].items['+i+'].'+myVar)

Yes, eval() it is. I tried that before without eval() that why it doesn’t work.

More question will come LOL

It wouldnt work like that because the . . . is not a string its a type of function, eval lets you feed a string into it meaning can use the ++.

:slight_smile:

Something like this should also work although not tried and might help keep things tidy;

var arrayValueString = 'r.orders['+o+'].items['+i+'].'+myVar;
return eval(arrayValueString)

The site I found eval() on said using it is bad practice but didn’t explain why but think it works well for this purpose.
The example didn’t specify the indexes so may have been a clashing issue maybe?

Time will tell lol. Working so far.

Now have to figure it out how to use curl in script. Reading Q post now.

Maybe I should wait for GraphQL. It would be crazy looping in rule. (order tags loop inside menu items loop inside create tickets loop) :weary:

1 Like

Thats the beauty of loops though, you only need to work it out for one item and loop it.

Give me few mins will whip up some bits to help you :wink:

Not quite sure how to deal with between tickets. Because It may need user interaction between ticket to make sure everything is correct.

It is bad practice. Because eval() “executes” what is inside it. So if you are receiving data from an outside source, the data could contain statements that could inadvertently destroy the planet.

Apparently, there are some legitimate use-cases for using eval(), but most people in this day and age will indicate that it is not necessary, save to overcome “lazy” coding practices.

In any case, you should try to avoid it… and there must be another way to use the fieldName even if you pass it in as a variable.

Are you sure there isn’t something in the syntax like this?

r.orders[o].items[i].field["id"]
… or …
r.orders[o].items[i].field["id"].value

So for your code:

r.orders[o].items[i].field[myVar]
… or …
r.orders[o].items[i].field[myVar].value

1 Like

Sorry, it doesn’t not work.
'orders[...].items[...].field' is null or not an object

Couple of ideas to help on your way.
You will still need these wether doing via loop or with new API and with API youll still need to script loops to generate the orders etc;

function orders() {
var array = ARRAY DATA
var parsedArray 		= JSON.parse(array);
var ordersCount			= parsedArray.orders.length;
var ordersIdListBuild	= "";
for (var o = 0; o<ordersCount;o++) {
	ordersIdListBuild	+= parsedArray.orders[o].id+',';
	}
var ordersIdList		= ordersIdListBuild.slice(0,-1)

return 'Order Count:'+ordersCount+' - Order List: '+ordersIdList
}

Your sample produces;

Order Count: 1 - Order List: 776113

function items(orderId) {
var array = ARRAY DATA
var parsedArray 		= JSON.parse(array);
var ordersCount			= parsedArray.orders.length;
var ordersIdListBuild	= "";
for (var o = 0; o<ordersCount;o++) {
	if (parsedArray.orders[o].id == orderId) {
		var orderIndex			= o;
		}
	}
var itemsCount			= parsedArray.orders[orderIndex].items.length;
var itemsIdListBuild	= "";
for (var i = 0; i<itemsCount;i++) {
	itemsIdListBuild	+= parsedArray.orders[orderIndex].id+',';
	}
var itemsIdList		= itemsIdListBuild.slice(0,-1)

return 'Items Count: '+itemsCount+' - Items List: '+itemsIdList
}

Produces this from sample with items('776113');

Items Count: 9 - Items List: 776113,776113,776113,776113,776113,776113,776113,776113,776113

1 Like