cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CAI Scripting syntax for array in json

k_varshney74
Explorer
0 Kudos
441

I have below json in memory-

[
{
"2 BHK":[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0]
},
{
"1 BHK":[0,1,0]
}
]

I want to show above data like below-

[
{
"bhk_type":"2 BHK"
"sold":2, (count of all the 1's)
"unsold":20 (count of all the 0's)
},
{
"bhk_type":"1 BHK"
"sold":1, (count of all the 1's)
"unsold":2 (count of all the 0's)
}
]

I am using pluck and @root but not getting correct result. Please help

View Entire Topic
Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

I did it this way, where memory.test holds the original string:

[{{#eachJoin memory.test}} {{#eachJoin @this}} { "bhk_type" : "{{@key}}", "sold": {{sum @this}} , "unsold": {{subtract (length @this) (sum @this) }} }{{/eachJoin}} {{/eachJoin}} ]

And here is the result

I assumed the string would always be in the exact format you provided, though varying BHK strings and length of number strings.

k_varshney74
Explorer
0 Kudos

Thank you!

Above solution is working fine.