Appian Community
Site
Search
Sign In/Register
Site
Search
User
DISCUSS
LEARN
SUCCESS
SUPPORT
Documentation
AppMarket
More
Cancel
I'm looking for ...
State
Not Answered
Replies
8 replies
Subscribers
8 subscribers
Views
5274 views
Users
0 members are here
Share
More
Cancel
Related Discussions
Home
»
Discussions
»
AI and Rules
Hello Everyone, Does anyone know of a way to get the a!fromJson function t
Jin Pheh
Certified Associate Developer
over 9 years ago
Hello Everyone,
Does anyone know of a way to get the a!fromJson function to respect arrays? Ie:
a!fromJson({"Values":["Alph;a","Beta","Delta"]})
will return a dictionary. When I index 'values' from the dictionary I get a single string representation of the array I started with instead of the array itself.
The problem appears to be that dictionaries themselves don't support arrays as values - for example this expression returns 1 instead of 3:
load(
local!dict: {Values: {1, 2, 3}},
length(local!dict.Values)
)
Does anyone have a way around this shortcoming?
OriginalPostID-197406
OriginalPostID-197406
Discussion posts and replies are publicly visible
0
sikhivahans
over 9 years ago
@jpheh One way I would generally prefer to do it is casting. Let's take the example which is quoted by you and this is how we can work around it:
load(
local!dict: {Values: {1, 2, 3}},
length(cast(101,local!dict.Values))
)
To the best of my knowledge, the structure {Values: {1, 2, 3}} is a custom structure created on-the-fly, and it is similar to label-value pairs. When it comes to CDT, we designate a field in CDT to hold particular type of data (such as Integer, Text, Text Multiple etc) at time of its creation. But in case of these Label Value pairs, they are just dictionaries that will store data in 'Any Type' format as we don't designate the data type of them. So we should cast the data to a desired data type prior to using the values in them.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
KARTHIK NATARAJAN
Certified Lead Developer
over 9 years ago
@jpheh as suggested by @sikhivahans we might need to convert the column to desired type before any evaluation. Please find in the modified code for the example you provided.
load(
local!dict: {Values: {1, 2, 3}},
length(apply(fn!tointeger,local!dict.Values))
)
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Stefan Helzle
A Score Level 3
over 9 years ago
To be more dynamic on datatypes you could use the reduce() function and a simple rule that just increments an integer by one.
load(
local!dict: {Values: {1, 2, 3, 4, "a", 1.5}},
reduce(rule!CountItemsForReduceFunction, 0, local!dict.Values)
)
CountItemsForReduceFunction(value (integer), item (any)):
=ri!value + 1
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Jin Pheh
Certified Associate Developer
over 9 years ago
Thanks Kathikn, your suggestion of using the apply even though the key/value pair in the dictionary is listed as a single value instead of an array appears to work.
For anyone searching in the future:
load(
local!dict: {Values: {1, 2, 3}},
length(apply(fn!tointeger,local!dict.Values))
)
will return an array of length 1 which is consistent with the behavior in my original post. However removing the length returns an array as I had originally expected.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Jin Pheh
Certified Associate Developer
over 9 years ago
Stefan, thanks for the suggestion however I'm actually looking for a more general solution. The use case I provided was a simplified example and was meant to demonstrate that even though I'm creating a dictionary where the value of the key is an array Appian is still reading it as a single value.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Stefan Helzle
A Score Level 3
over 9 years ago
Sure, this is one of the edge cases where data type handling in Appian is not very consistent. Just try not(null) and if(null, 1, 0).
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Jin Pheh
Certified Associate Developer
over 9 years ago
ha, I completely agree. I have a whole list of appian-isms myself but this is the first time I've had to do anything even remotely complex with dictionaries/JSON.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
kshamag498
over 9 years ago
Hi jpheh,
You can use the split() function to get the array returned from the single valued string. For example -
load(
local!dict: {Values: {1, 2, 3}},
length(split(local!dict.Values,";"))
)
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel