cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Mapping (Extracting) values from xml files with mapper

SL12345
New Contributor III

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

1 ACCEPTED SOLUTION

j_angelevski
Contributor III

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
    }
]

View solution in original post

1 REPLY 1

j_angelevski
Contributor III

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
    }
]