Get boolean array from json

Hi at all,
Tested with Ignition 8.1.7 and also with new 8.1.9 …

i Have this json tag:

I need to get boolean array complete (all 32 element), for example DI.Auto.ARR from json format, but i have this error:

Error_TypeConversion(“Cannot coerce value ‘[ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true ]’ into type: class [Ljava.lang.Boolean;”)

If i get only one element array , for example DI.Auto.ARR[1] it work fine.

In this screenshot ARRAY is complete array derived from json data type Boolean Array , and ARRAY 1 is array with only [1] element that work fine.

How can i solve this issue ? Thanks

you tried to turn an array into a boolean
you will have to loop over the value you got and append it to a list

list = []
for b in DI.Auto.ARR:
 #might have to convert b to boolean here if it gets a string

  list.append(b)
return list

Hi, thanks for answer
ok for script , but i need to put a boolean array in UDT , and not all element with index…

2021-08-10 12_03_13-Window

Now i make each tag for each index ARR_00 = index 0 DI.Auto.Arr[0] ecc…

I need to make only one tag DI.Auto.ARR in boolean array format, to make access direct with index, but i don’t know how…

could you past the json string here and then a string of what you need?(no need to put in every true or false)

Json String :

{ “Misc”: { “ManuActive”: false }, “DI”: { “Cnfg”: { “ARR”: [ true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] }, “Raw”: { “ARR”: [ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] }, “Mon”: { “ARR”: [ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] }, “Moff”: { “ARR”: [ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] }, “Auto”: { “ARR”: [ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true ] }, “Work”: { “ARR”: [ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] } }, “DO”: { “Cnfg”: { “ARR”: [ true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] }, “Raw”: { “ARR”: [ true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] }, “Mon”: { “ARR”: [ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] }, “Moff”: { “ARR”: [ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ] }, “Auto”: { “ARR”: [ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true ] }, “Work”: { “ARR”: [ true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false ] } }, “AI”: { “Cnfg”: { “ARR”: [ true, false, false, false ] }, “ForceCmd”: { “ARR”: [ false, false, false, false ] }, “RawVal”: { “ARR”: [ 0.0, 0.0, 0.0, 0.0 ] }, “ForceVal”: { “ARR”: [ 0.0, 0.0, 0.0, 0.0 ] }, “WorkVal”: { “ARR”: [ 0.0, 0.0, 0.0, 0.0 ] } }, “AO”: { “Cnfg”: { “ARR”: [ true, true, false, false ] }, “ForceCmd”: { “ARR”: [ false, false, false, false ] }, “RawVal”: { “ARR”: [ 0.0, 0.0, 0.0, 0.0 ] }, “ForceVal”: { “ARR”: [ 0.0, 0.0, 0.0, 0.0 ] }, “WorkVal”: { “ARR”: [ 0.0, 0.0, 0.0, 0.0 ] } } }

And i need for example boolean array tag that point to json: DI.Auto.ARR , with an unique tag in array format contain all value.

So like this?


image

json = system.util.jsonDecode(value)
boolArry = json['DI']['Auto']['ARR']
system.tag.write("[default]arrytag",boolArry)

(i forgot, i use VISION)

Yes, but i need directly in UDT, recover json with derived tag with JsonGet and JsonSet, is possible without script directly in UDT with expression?
_jsonUdt is document json to derive

In this example i derive for each tag ARR_XX the index with jsonGet({source},“DI.AUTO.ARR[XX]”)

This is the example Tag ARRAY not work…

Seems its reading the value as a string instead of an array.
you and do this, tho i find it not pretty xd, i think getting a script and use runScript would look better and would be easier to use in a udt
split(substring(jsonGet({source},"DI.Auto.ARR"),'1',len(jsonGet({source},"DI.Auto.ARR"))-1),",")

It work but make me array[3] instead of [32]…
But the other problem then is the write expression jsonSet… how to do this?

So , your method i think is better, but i don’t know how to use script inside UDT, i use vision.

Can you tell me how?
thanks

def toBoolArr(value,x,y):
	json = system.util.jsonDecode(value)
	boolArry = json[x][y]['ARR']
	return boolArry


runScript("NewScript.toBoolArr",0,{source} ,'DI','Auto')
image

feel free to add in ARR as variable aswell

Ok i will try… thanks…
This code to substitute jsonGet… for jsonSet ?

Thanks

i guess so,
converting from array to string should atleast go easier

Ok thanks for all, i will read manual to try to convert back array…

Thanks so much… i will tell you if i solve this problem :slight_smile:

1 Like

I try, i don’t know tell me Error Type conversion, i do the same script :sweat:

where is issue?

what does it say it cannot convert from?

This is the error type:

Same error…
I think script not run because need to run al gateway level to modify tag, i try to set “gateway scripting project” to name of my project but not work.

heh idk what else to do with that sry.
ive not used tags that much yet, and in perspective i default to a transform scipt instead of expressions xd
i guess you will have to use the split afterall

Ok, but with split i don’t know how not recover all 32 bool and only 3

which 3 values did you get?
that script worked for me as shown in the screen shot.
turn the tag into a dataset type and show me whats in it when you use the script

Return this in dataset… (versione 8.1.9)

In version 8.1.7 return an error :