0
votes

The following format orders data is in payload.

{
    "orders" : {
        "order" : [ {
            "id" : "4358294728",
            "fulfillment" : {
                "tracking_number" : "917",
                "line-items" : {
                    "id" : "8367649608"
                }
            }
        }, {
            "id" : "4358301768",
            "fulfillment" : {
                "tracking_number" : "918",
                "line-items" : [ {
                    "id" : "8367663240"
                }, {
                    "id" : "8367663304"
                }, {
                    "id" : "8367663368"
                } ]
            }
        } ]
    }
}

i want to get the total number. orders from this payload. I am using #[message.payload.orders.order.size()] for this. It is giving right output.

But for a single order it is giving wrong the output = 2. But it will be 1.

{
    "orders" : {
        "order" : {
            "id" : "4358153416",
            "fulfillment" : {
                "tracking_number" : "915",
                "line-items" : {
                    "id" : "8367362760"
                }
            }
        }
    }
}

Can you please help me to get the desired output?

2

2 Answers

1
votes

I think the problem is in the structure of your json. In the second one:

{
    "orders" : {
        "order" : {
            "id" : "4358153416",
            "fulfillment" : {
                "tracking_number" : "915",
                "line-items" : {
                    "id" : "8367362760"
                }
            }
        }
    }
}

The item order is not an array, so it gives '2' as result because it has two elements inside it: id and fullfillment.

You can solve this, by having your json properly generated, it would be like this (check de '[ ]' ):

{
    "orders" : {
        "order" : [{
            "id" : "4358153416",
            "fulfillment" : {
                "tracking_number" : "915",
                "line-items" : {
                    "id" : "8367362760"
                }
            }
        }]
    }
}

Also, the item order doesn't have to be explicit. You just need an array of orders. The original json should be:

{
    "orders" : [ 
         {
            "id" : "4358294728",
            "fulfillment" : {
                "tracking_number" : "917",
                "line-items" : {
                    "id" : "8367649608"
                }
            }
        }, 
        {
            "id" : "4358301768",
            "fulfillment" : {
                "tracking_number" : "918",
                "line-items" : [ {
                    "id" : "8367663240"
                }, {
                    "id" : "8367663304"
                }, {
                    "id" : "8367663368"
                } ]
            }
        } 
      ]   
}
0
votes

I am getting correct number with the following config:-

 <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
  <flow name="Flow1" >
    <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
    <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>
    <logger message="Order size #[message.payload.orders.order.size()] " level="INFO" doc:name="Logger"/>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
   </flow>   

and yes, your JSON is incorrect.. It should be :-

{
    "orders" : {
        "order" :[ {
            "id" : "4358153416",
            "fulfillment" : {
                "tracking_number" : "915",
                "line-items" : {
                    "id" : "8367362760"
                }
            }
        }]
    }
}

You have missed [ after order