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:
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.
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:
- You're on a recent enough version (8.1.8+)
- Your JSON data is actually in JSON format, e.g.
{"foodtray":{"fruits":["apple","mango","banana"],"vegetables":["brinjal","eggplant", "apple"]}}
- 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.
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.
@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.
+1
If you can share your code and perhaps some sample data, we can help investigate further.
{
"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
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.
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.
Didn't know you could do this!
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!