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
6 replies
Subscribers
8 subscribers
Views
10603 views
Users
0 members are here
Share
More
Cancel
Related Discussions
Home
»
Discussions
»
AI and Rules
JSON Key Lookup
kdimitrov
over 8 years ago
Hi there,
Curious if anyone has any good suggestions on the below problem. We are trying to get the keys from a JSON array object. The keys unfortunately are dates and are dynamic. For example, the below works fine, but it's hard-coding "09/03/2017". Any way to get all the keys from the object properly? Worth noting that index(local!list, 1) does not work in the below case.
=with(
local!json: "{
""09/03/2017"": {
""total"": 5
},
""10/03/2017"": {
""total"": 5
}
}",
local!list: a!fromJson(local!json),
index(local!list, "09/03/2017")
)
OriginalPostID-268859
Discussion posts and replies are publicly visible
0
PhilB
A Score Level 1
over 8 years ago
I was going to suggest using a!jsonPath, but it appears it's impossible to get the key names in that manner. Arguably the JSON here, whilst syntactically valid, isn't really logically valid. However, it is possible to extract the keys from the JSON using a regex, and then loop over them - but it does require the regex plugin:
= with(
local!json: "{
""09/03/2017"": {
""total"": 5
},
""10/03/2017"": {
""total"": 5
}
}",
apply(
fn!index(
a!fromJson(
local!json
),
_,
null
),
fn!regexallmatches(
"([0-9]{2}\\/[0-9]{2}\\/[0-9]{4})",
local!json
)
)
)
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
PhilB
A Score Level 1
over 8 years ago
Just FYI, to access the actual values in the response, just use dot notation (ie .total) on the closing bracket of the apply, or wrap the apply in an index().
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
PhilB
A Score Level 1
over 8 years ago
Oh and I should add that the date regex could definitely be tightened up - but works ok for this example.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Josh
Certified Lead Developer
over 8 years ago
Here is an Appian implementation for your data model. Note that it could probably be cleaned up and it may not work for other data models! Also, you want the "values" here, the "keys" are the dates in your dictionary.
with(
local!json: "{
""09/03/2017"": {
""total"": 5
},
""10/03/2017"": {
""total"": 5
}
}",
local!dictionary: a!fromJson(local!json),
local!objects: split(tostring(local!dictionary), ","),
local!keysRaw: apply(
mid(_, 1, 11),
local!objects
),
local!keysStripped: apply(stripwith(_, "[]:"), local!keysRaw),
apply(index(local!dictionary, _, {}), local!keysStripped)
)
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
shubhamg0005
over 6 years ago
[Try this rule . I have made json from myself and it will gives all the key from Json ]
a!forEach(
items: split(
a!toJson(
{
x: 1,
y: 2,
z: 3
}
),
","
),
expression: split(fv!item,"""")[2])
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
vijay
over 6 years ago
Hi, Please find the below code, little lengthy but might help to your requirement:
with(
local!json: "{
""09/03/2017"": {
""total"": 5
},
""10/03/2017"": {
""total"": 10
}
}",
/*Convert into Appian value*/
local!data: a!fromJson(
local!json
),
/*Split to fetch key type date values*/
local!divideTheData: split(
split(
local!data,
":"
),
","
),
/*To remove unwanted characters*/
local!updatedData: a!forEach(
local!divideTheData,
stripwith(
fv!item,
"["
)
),
local!fetchDesiredData: a!forEach(
local!updatedData,
if(
find(
"/",
fv!item
),
true(),
null
)
),
index(
local!updatedData,
where(
local!fetchDesiredData
)
)
)
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel