Created 07-29-2022 03:55 AM
Hello!
I've read this and this and this but explanations are pretty scarce. I understand what "&" means. But I can't wrap my head around what [&] means =\
Can someone please explain me what this [&] does applicable to this example? What exactly happening in lines? :
"@1": "outer[&4].inner[&2].t",
"@(3,eventTypeCode)": "outer[&4].inner[&2].etc"
JSON:
{
"uID": 1000358546,
"events": [{
"eventTypeCode": "FEEDBACK",
"transports": ["PUSH", "SMS"]
}, {
"eventTypeCode": "MARKETING",
"transports": ["PUSH", "EMAIL"]
}, {
"eventTypeCode": "ORDER_STATUS",
"transports": ["SOC_VK"]
}
]
}
SPEC:
[{
"operation": "shift",
"spec": {
"events": {
"*": {
"transports": {
"*": {
"*": {
"@1": "outer[&4].inner[&2].t",
"@(3,eventTypeCode)": "outer[&4].inner[&2].etc"
}
}
}
}
},
"*": "&"
}
}]
RESULT:
{
"uID" : 1000358546,
"outer" : [ {
"inner" : [ {
"t" : "PUSH",
"etc" : "FEEDBACK"
}, {
"t" : "SMS",
"etc" : "FEEDBACK"
} ]
}, {
"inner" : [ {
"t" : "PUSH",
"etc" : "MARKETING"
}, {
"t" : "EMAIL",
"etc" : "MARKETING"
} ]
}, {
"inner" : [ {
"t" : "SOC_VK",
"etc" : "ORDER_STATUS"
} ]
} ]
}
Thanks beforehand!
Created on 07-31-2022 05:20 PM - edited 07-31-2022 11:26 PM
"&" on the RHS returns the value found at the specified path position. When used inside "[]" the meaning is the same and the returned value is used as an index of that array.
For example, take this expression:
"@1": "outer[&4].inner[&2].t",
The different matches for "&" expressions are shown below:
[{
"operation": "shift",
"spec": {
"events": {
"*": { <-- &4 matches the position of the item in the events array (e.g. 0, 1, 2)
"transports": { <-- &3 matches "transports"
"*": { <-- &2 matches the position of the item in the transports array (e.g. 0, 1)
"*": { <-- &1 matches the same as &0
"@1": "outer[&4].inner[&2].t", <-- &0 matches the value of the item in the transports array (e.g. PUSH, SMS)
"@(3,eventTypeCode)": "outer[&4].inner[&2].etc"
}
}
}
}
},
"*": "&"
}
}]
So, the expression "outer[&4].inner[&2].t" will create an "outer" array that has as many elements as the "events" array. Each element of the "outer" array will have an "inner" attribute whose value will be another array. This array will have as many elements as the respective "transports" array, each one with the "t" and "etc" attributes.
Hope this helps.
Cheers,
André
Created on 08-01-2022 06:22 AM - edited 08-01-2022 02:53 PM
1. It depends on the context and the level of &n. In the example above, &1 return the element in the transports array (e.g. "PUSH"), while &2 returns the numeric index of that element in the array (e.g. 0).
2. &4 and &2 are numeric array indexes. outer[&4] means that the output will be in the &4 position of an array called outer. That element of the array will have and attribute called inner and the &2 position of the inner array will have two attributes, t and etc, with the specified values.
Cheers,
André
Created on 07-31-2022 05:20 PM - edited 07-31-2022 11:26 PM
"&" on the RHS returns the value found at the specified path position. When used inside "[]" the meaning is the same and the returned value is used as an index of that array.
For example, take this expression:
"@1": "outer[&4].inner[&2].t",
The different matches for "&" expressions are shown below:
[{
"operation": "shift",
"spec": {
"events": {
"*": { <-- &4 matches the position of the item in the events array (e.g. 0, 1, 2)
"transports": { <-- &3 matches "transports"
"*": { <-- &2 matches the position of the item in the transports array (e.g. 0, 1)
"*": { <-- &1 matches the same as &0
"@1": "outer[&4].inner[&2].t", <-- &0 matches the value of the item in the transports array (e.g. PUSH, SMS)
"@(3,eventTypeCode)": "outer[&4].inner[&2].etc"
}
}
}
}
},
"*": "&"
}
}]
So, the expression "outer[&4].inner[&2].t" will create an "outer" array that has as many elements as the "events" array. Each element of the "outer" array will have an "inner" attribute whose value will be another array. This array will have as many elements as the respective "transports" array, each one with the "t" and "etc" attributes.
Hope this helps.
Cheers,
André
Created 08-01-2022 04:56 AM
Hello! Thank you for your comprehensive answer. But I sitll don't quite understand two things.
1. I am confused what does wildcard & do? Does it return only key or does it return the indices of elements as well, depending on level of &n?
2. Are [&4] and [&2] numbers like indices in arrays or strings like in dictionary "key":"value" pairs.?
Thank you beforehand!
Created on 08-01-2022 06:22 AM - edited 08-01-2022 02:53 PM
1. It depends on the context and the level of &n. In the example above, &1 return the element in the transports array (e.g. "PUSH"), while &2 returns the numeric index of that element in the array (e.g. 0).
2. &4 and &2 are numeric array indexes. outer[&4] means that the output will be in the &4 position of an array called outer. That element of the array will have and attribute called inner and the &2 position of the inner array will have two attributes, t and etc, with the specified values.
Cheers,
André