# Iterate Using Expression

**URL:** <https://forum.inductiveautomation.com/t/iterate-using-expression/56755>\
**Category:** Ignition\
**Created:** [March 3, 2022, 12:36am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755 "2022-03-03T00:36:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shruty\_balaji](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@shruty\_balaji](https://forum.inductiveautomation.com/u/shruty_balaji)\
**Post date:** [March 3, 2022, 12:36am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/1 "2022-03-03T00:36:28Z")

</div>

How do i iterate over a list using Ignitions expression language . I know i can iterate using script , but the project needs me to use expression .

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 3, 2022, 12:48am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/2 "2022-03-03T00:48:33Z")

</div>

This sounds like an XY problem.

What are you trying to do in this iteration, and why do you think you can’t use a script?

---

<div class="post-metadata">

**Author:** ![shruty\_balaji](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@shruty\_balaji](https://forum.inductiveautomation.com/u/shruty_balaji)\
**Post date:** [March 3, 2022, 12:54am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/3 "2022-03-03T00:54:24Z")

</div>

I have a jason file of the format  
{foodtray:[fruits:{apple,mango,banana},vegetables:{brinjal,eggplant }]}  
I am trying to get the iterate through the foodtray list and find if apple is present.

The reason i cant use scripting is the speed … My Jason file has list of 10 items and i need to use 50 such jason files on my ignition project.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [March 3, 2022, 1:34am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/4 "2022-03-03T01:34:44Z")

</div>

Regardless of speed, you could use an [indexOf expression](https://docs.inductiveautomation.com/display/DOC81/indexOf) result that isn’t `-1` to indicate the string is found:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/6/d6d86b3b1f9bdc57805ceb5fa2b0f6b5e90d5fb8.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/e/be83390ba97cab4c159d4469957ed8c2816e7c63.png)

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [March 3, 2022, 1:55am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/5 "2022-03-03T01:55:43Z")

</div>

I have heard that scripting is faster than expressions

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 3, 2022, 9:26am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/6 "2022-03-03T09:26:21Z")

</div>

The execution time might be, but I suspect there's some overhead to running jython.

> [@shruty\_balaji](#):
>
> The reason i cant use scripting is the speed … My Jason file has list of 10 items and i need to use 50 such jason files on my ignition project.

Frankly, I don't see lists of 10 items being too long to process...  
Also, you might have a way of processing all your json in just one script.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 3, 2022, 12:09pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/7 "2022-03-03T12:09:14Z")

</div>

> [@jespinmartin1](#):
>
> I had heard that scripting if faster than expressions

Not so.

> [@Binding Transforms Best Practices](https://forum.inductiveautomation.com/t/binding-transforms-best-practices/24899/2):
>
> Seat of the pants answer with no performance testing to back it up: native transforms (ie, mapping) will be faster than expression transforms will be faster than script transforms “Native” transforms are executing as direct Java code in and out. Expressions have to be parsed and calculated in steps, so there’s more lookups and variable timing involved. Scripts will almost certainly be the slowest - we have to either grab or create a Python interpreter, compile the function you provide (we …

However, iterating through a json file with 10 items in script is in the ms range of speed, I highly doubt performance is a concern with even 500 files of that size throughout a project. Unless you’re iterating through the files in the same script.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 3, 2022, 4:58pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/8 "2022-03-03T16:58:46Z")

</div>

> “The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.”

If you implement it and prove that scripting is unacceptably slow, then I would worry about expressions.

That said, assuming:

1. You're on a recent enough version (8.1.8+)
2. Your JSON data is actually _in_ JSON format, e.g. `{"foodtray":{"fruits":["apple","mango","banana"],"vegetables":["brinjal","eggplant", "apple"]}}`
3. Your JSON data is guaranteed to always have the same structure

You _can_ use expressions to safely check for a given key:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/1/31785b4655c942bad4836fe544dc8a29ad900954.png)  
The result will be `-1` if the given element is not found in the list (`jsonGet` with a path of `foodtray.fruits` will return the array of fruits).

This would be safer (but _significantly_ slower) than @witman's raw `indexOf` on the overall string.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [March 3, 2022, 5:18pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/9 "2022-03-03T17:18:58Z")

</div>

> [@paul-griffith](#):
>
> This would be safer (but _significantly_ slower) than @witman’s raw `indexOf` on the overall string.

Yes, this version would avoid finding `"apple X"` in other locations--for example `"apple pie"` in `foodtray.desserts`. You'd want this distinction if using this to pick what goes on the trays. On the other hand, if you're checking food allergies, the string comparison would do.

It does seem unlikely scripting would be too slow for this.

---

<div class="post-metadata">

**Author:** ![shruty\_balaji](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@shruty\_balaji](https://forum.inductiveautomation.com/u/shruty_balaji)\
**Post date:** [March 3, 2022, 8:10pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/10 "2022-03-03T20:10:40Z")

</div>

@lrose yes, I am iterating through the files in the same script which is causing significant delay with scripting .

---

<div class="post-metadata">

**Author:** ![shruty\_balaji](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@shruty\_balaji](https://forum.inductiveautomation.com/u/shruty_balaji)\
**Post date:** [March 3, 2022, 8:15pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/11 "2022-03-03T20:15:32Z")

</div>

Another issue is - the structure of jason file is not fixed ( i.e fruits ,vegetables can change positions ) . If "apple " found, i need to replace apple with "apple found "

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [March 3, 2022, 8:22pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/12 "2022-03-03T20:22:00Z")

</div>

If all you need to do is replace “apple” with “apple found” and you don’t have do worry about replacing “apple” in “apple found” and ending up with “apple found found”, [replace expression](https://docs.inductiveautomation.com/display/DOC81/replace) will do that. If you need to prevent replacing “apple” in “apple found”, you’d want the replace conditional on indexOf not finding “apple found”.

I don’t understand the end goal here. If you’re able to share more details, we may have better suggestions.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 3, 2022, 8:27pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/13 "2022-03-03T20:27:25Z")

</div>

> [@witman](#):
>
> If you’re able to share more details, we may have better suggestions.

+1

If you can share your code and perhaps some sample data, we can help investigate further.

---

<div class="post-metadata">

**Author:** ![shruty\_balaji](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@shruty\_balaji](https://forum.inductiveautomation.com/u/shruty_balaji)\
**Post date:** [March 3, 2022, 11:10pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/14 "2022-03-03T23:10:20Z")

</div>

```auto
{
  "food": "types of food",
  
  },
  "types": [
    {
      "name": "fruits",
      "display": {
        "value": "String"
      },
      "value": "apple"
    },
    {
      "name": "vegetables",
      "display": {
        "value": "string"
      },
      "value": "potato","cabbage"
      "
    },
    {
      "name": "pulses",
      "display": {
        "value": "string"
      },
      "value": "sprouts",
    
    },
    {
      "name": "dairy",
      "display": {
        "value": "products"
      },
      "value": milk,butter,yogurt
      
    },
    {
      "name": "others",
      "display": {
        "value": "string"
      },
      "value": tomato,pumpkin
      
    },
    {
      "name": "drinks_na",
      "display": {
        "value": "non alcohol"
      },
      "value":"beer,"rum"
     
    },
    {
      "name": "chips",
      "display": {
        "value": "string"
      },
      "value": lays,cheetos
      
    },
   
    }
  ]
}

```

This is the exact format of my jason file . I have to pull 50 such jason file to my project.i need to check if fruits is present in list and if so i need to output “apple” to a label

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 4, 2022, 12:50am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/15 "2022-03-04T00:50:40Z")

</div>

> [@shruty\_balaji](#):
>
> This is the exact format of my jason file

Perhaps you made a typo, because I don’t believe this valid JSON

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 4, 2022, 7:59am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/16 "2022-03-04T07:59:32Z")

</div>

It’s **JSON** , not ‘jason’ (although that’s how we pronounce it).

There are multiple errors in your JSON. Expand the summary below.

> **Summary**
>
> ```auto
> {
> "food": "types of food",
>   
> }, <---- error - extra }
> "types": [
> {
> "name": "fruits",
> "display": {
> "value": "String"
> },
> "value": "apple"
> },
> {
> "name": "vegetables",
> "display": {
> "value": "string"
> },
> "value": "potato","cabbage"
> " <---- error stray "
> },
> {
> "name": "pulses",
> "display": {
> "value": "string"
> },
> "value": "sprouts",
>     
> },
> {
> "name": "dairy",
> "display": {
> "value": "products"
> },
> "value": milk,butter,yogurt <---- error - missing ""
>       
> },
> {
> "name": "others",
> "display": {
> "value": "string"
> },
> "value": tomato,pumpkin <---- error - missing ""
>       
> },
> {
> "name": "drinks_na",
> "display": {
> "value": "non alcohol"
> },
> "value":"beer,"rum" <---- error - extra "
>      
> },
> {
> "name": "chips",
> "display": {
> "value": "string"
> },
> "value": lays,cheetos <---- error - missing ""
>       
> },
>    
> }
> ]
> }
> 
> ```

Your first example is also invalid JSON. Every key and value is missing quotes `"`. e.g.

> **Summary**
>
> ```auto
> {
> foodtray: [fruits: {
> apple,
> mango,
> banana
> }, vegetables: {
> brinjal,
> eggplant
> }]
> }
> 
> ```

This might be what you are trying to do:

> **Summary**
>
> ```auto
> {
> "foodtray": {
> "fruits": [
> "apple",
> "mango",
> "banana"
> ],
> "vegetables": [
> "brinjal",
> "eggplant"
> ]
> }
> }
> 
> ```

You need to pay a lot more detail to correct indentation if you want to make your JSON readable so you can find errors.

You can past your JSON into [https://jsonlint.org](https://jsonlint.org) to have it validated.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 7, 2022, 8:32am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/17 "2022-03-07T08:32:32Z")

</div>

I’m curious to see the code behind this. I can’t imagine 50 files containing 10 items taking any noticeable amount of time to process, unless said processing is in itself quite heavy.

Another point: that json is an abomination.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [March 7, 2022, 9:16am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/18 "2022-03-07T09:16:23Z")

</div>

Didn't know you could do this! 🤙

> [@shruty\_balaji](#):
>
> ```auto
> {
> "name": "drinks_na",
> "display": {
> "value": "non alcohol"
> },
> "value":"beer,"rum"
>      
> }
> 
> ```

Non-alcoholic beer and rum??

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 7, 2022, 9:54am UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/19 "2022-03-07T09:54:38Z")

</div>

> [@nminchin](#):
>
> Non-alcoholic beer and rum??

You'd be surprised. I've even encountered non-alcoholic wine, which kind of offends me.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 7, 2022, 2:10pm UTC](https://forum.inductiveautomation.com/t/iterate-using-expression/56755/20 "2022-03-07T14:10:17Z")

</div>

> [@nminchin](#):
>
> Non-alcoholic beer and rum??

You can't. It won't parse!
