02-03-2023 06:08 AM
Hi Snaplogic experts,
i would like to extract values from xml files. lets say:
there is a check if Value1 is object, if it is true, than do this otherwise do something else …
typeof jsonPath($, "$..Value1")) == 'object' ? jsonPath($, "$...Value1.Value2") : ''
but there is possibility Value1 will be array and Value2 will be object
My question is:
What is the best approach to check if Value1 is object ? if yes, check if Value2 is object. If Value1 is not object check if it is array and Value2 is object or array?
is it good approach do it in one mapper or condition will be too long/slow to solve it ?
Is it possible to check (in one step) if all Value1.Value2 are objects?
Thank you
Solved! Go to Solution.
02-03-2023 07:38 AM
Hi @SL12345,
You can use the match operator: https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/543621269/The+match+Control+Operator.
In your case it can be:
match typeof $Value1 {
"object" => "Your logic goes here if $Value1 is an object",
"array" => "Your logic goes here if $Value1 is an array",
_ => "Your logic goes here if the $Value1 is neither an object nor an array"
}
For example, if I have this input data:
[
{
"data": {
"one": 1,
"two": 2
}
},
{
"data": [1, 2]
}
]
In this case, let’s say that we want to get the key ‘one’ if the ‘data’ is an object and get the second value if the ‘data’ is an array, I can do the following:
match typeof $data {
"object" => $data["one"],
"array" => $data[1],
_ => ""
}
The output would be:
[
{
"output":1 // This is from data["one"], it retrieves the value of the key 'one'
},
{
"output":2 // This if from data[1], it retrieves the second value from the array
}
]
02-03-2023 07:38 AM
Hi @SL12345,
You can use the match operator: https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/543621269/The+match+Control+Operator.
In your case it can be:
match typeof $Value1 {
"object" => "Your logic goes here if $Value1 is an object",
"array" => "Your logic goes here if $Value1 is an array",
_ => "Your logic goes here if the $Value1 is neither an object nor an array"
}
For example, if I have this input data:
[
{
"data": {
"one": 1,
"two": 2
}
},
{
"data": [1, 2]
}
]
In this case, let’s say that we want to get the key ‘one’ if the ‘data’ is an object and get the second value if the ‘data’ is an array, I can do the following:
match typeof $data {
"object" => $data["one"],
"array" => $data[1],
_ => ""
}
The output would be:
[
{
"output":1 // This is from data["one"], it retrieves the value of the key 'one'
},
{
"output":2 // This if from data[1], it retrieves the second value from the array
}
]