Iterate Using Expression

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 .

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?

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.

Regardless of speed, you could use an indexOf expression result that isn’t -1 to indicate the string is found:

1 Like

I have heard that scripting is faster than expressions

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

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.

2 Likes

Not so.

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.

“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:


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.

4 Likes

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.

3 Likes

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

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 "

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 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.

3 Likes

+1

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

5 Likes
{
  "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

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

4 Likes

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

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

Summary
{
  "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
{
	foodtray: [fruits: {
		apple,
		mango,
		banana
	}, vegetables: {
		brinjal,
		eggplant
	}]
}

This might be what you are trying to do:

Summary
{
	"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 to have it validated.

4 Likes

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.

2 Likes

Didn’t know you could do this! :call_me_hand:

Non-alcoholic beer and rum??

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

You can’t. It won’t parse!

1 Like