SMSVikasK
SMSVikasK

Reputation: 23

XML to JSON - Liquid Mapping - 2 level XML repeating records - Logic Apps

Looking for your feedback on my liquid template mapping script, I cannot parse 2nd level order within in product. I am new to the syntax trying to parse as per the expected output. Looking for your suggestions on how to fix, and handle second-level repeating records.

Looking for your feedback on my liquid template mapping script, I cannot parse 2nd level order within in product. I am new to the syntax trying to parse as per the expected output. Looking for your suggestions on how to fix, and handle second-level repeating records.

Input XML

<Products>
    <Product>
        <productname>iPhone</productname>
        <productid>EL123</productid>
        <productcategory>Electronics</productcategory>
        <order>
            <sold_to_customer_number>33</sold_to_customer_number>
            <ship_to_customer_number>1</ship_to_customer_number>
        </order>
    </Product>
    <Product>
        <productname>OnePlus</productname>
        <productid>EL124</productid>
        <productcategory>Electronics</productcategory>
        <order>
            <sold_to_customer_number>2</sold_to_customer_number>
            <ship_to_customer_number>2</ship_to_customer_number>
        </order>
        <order>
            <sold_to_customer_number>3</sold_to_customer_number>
            <ship_to_customer_number>3</ship_to_customer_number>
        </order>
    </Product>
</Products>

Expected JSON

{
    "Products": [
        {
            "productname": "iPhone",
            "productid": "EL123",
            "productcategory": "Electronics",
            "order": [
                {
                    "sold_to_customer_number": "33",
                    "ship_to_customer_number": "1"
                }
            ]
        },
        {
            "productname": "OnePlus",
            "productid": "EL124",
            "productcategory": "Electronics",
            "order": [
                {
                    "sold_to_customer_number": "2",
                    "ship_to_customer_number": "2"
                },
                {
                    "sold_to_customer_number": "3",
                    "ship_to_customer_number": "3"
                }
            ]
        }
    ]
}

Liquid Tempalte - Issue with Order to parse

{
         "Products":[
             {% for item in content.Products %}         
                { 
                     "productname":"{{item.productname}}",
                     "productid":"{{item.productid}}",
                     "productcategory":"{{item.productcategory}}",
                     "order":[
                             {% for item in content.Products.Product %}         
                                { 
                                     "sold_to_customer_number":"{{item.order.sold_to_customer_number}}",
                                      "ship_to_customer_number":"{{item.order.ship_to_customer_number}}"
                                     
                                },
                             {% endfor %}
                     ]
                },
            {% endfor %}
         
         ]
}

Thanks SMSVikasK

Upvotes: 1

Views: 414

Answers (2)

SMSVikasK
SMSVikasK

Reputation: 23

Thanks for your input. It helps. I also tried something to have a working solution to continue to progress.

what you mentioned is right, I tried all options and ended up as per below to sort my issue, and it's working for me as per expected.

  • XML to JSON conversion using logic app expressions.
  • JSON to JSON liquid mapping transformation.
  • JSON would have a repeating array or single object mix of both as per the above sample ie Products.
  • I am trying to loop for both array and object with the if condition.
  • below is a sample solution for similar objects.

the below steps/solution is working as expected.

{
 "ETA_STATUS_CODE": "{{content.ETA_STATUS_CODE}}",
                        
    {% if content.ORDER.size >= 1 %}
       "ORDER":[
           {% for ord in content.ORDER %}           
                { 
                  "SOLD_TO_CUSTOMER_NUMBER": "{{ ord.SOLD_TO_CUSTOMER_NUMBER }}",
                },
            {% endfor %}
            ],
    {% else %}
        "ORDER":[                            
            { 
                "SOLD_TO_CUSTOMER_NUMBER": "{{ content.ORDER.SOLD_TO_CUSTOMER_NUMBER }}",
                                                             
            }
        ]                            
    {% endif %}
}

Upvotes: 1

Skin
Skin

Reputation: 11197

If you can't solve your issue (I've tried and I can't either) using Liquid then I'd look at using the Advanced Data Operations connector, it will do it for you.

The only thing it doesn't do is wrap the resulting primary array inside a Products property. The Compose step at the end does that though.

You'll also notice that I've changed the names of the destination properties slightly to show the distinct difference between the source and target.

This basic flow demonstrates how ...

Flow

This is the resulting JSON (noting that it appears to have ordered the properties alphabetically but that shouldn't be an issue) ...

Result

Upvotes: 1

Related Questions