Api Connect v10 map json message with object array to object using foreach or similar (map policy)

414 Views Asked by At

I'm new to API Connect, and I haven't been able to find the correct mapping to pass from an array of objects to an object, evaluating its content. I explain:

I have as input a json like this:

{
"methodCall": {
    "methodName": {
        "$": "ThisIsTheMethodName"
    },
    "params": {
        "param": {
            "value": {
                "array": {
                    "data": {
                        "value": {
                            "struct": {
                                "member": [
                                    {
                                        "name": {
                                            "$": "message"
                                        },
                                        "value": {
                                            "string": {
                                                "$": "Some text to send to client"
                                            }
                                        }
                                    },
                                    {
                                        "name": {
                                            "$": "phone"
                                        },
                                        "value": {
                                            "string": {
                                                "$": "9876543120124"
                                            }
                                        }
                                    },
                                    {
                                        "name": {
                                            "$": "date"
                                        },
                                        "value": {
                                            "string": {}
                                        }
                                    },
                                    {
                                        "name": {
                                            "$": "appid"
                                        },
                                        "value": {
                                            "string": {
                                                "$": "Application Identificator"
                                            }
                                        }
                                    },
                                    {
                                        "name": {
                                            "$": "costCenter"
                                        },
                                        "value": {
                                            "string": {
                                                "$": "102030"
                                            }
                                        }
                                    },
                                    {
                                        "name": {
                                            "$": "filled"
                                        },
                                        "value": {
                                            "string": {
                                                "$": "filledString"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}

}

and I need to generate this json output from the mapping:

{
"phoneNumberSMS":"983849780",

"message":"Some text to send to client",

"date": "2022-10-04T15:30:00",

"appId":"Application Identificator",

"costCenter":"102030",

"filled":"filledString" }

I have tried with the following configuration, but without success:

On the YAML

  actions:
- set: output.phoneNumberSMS
  foreach: input.methodCall.params.param.value.array.data.value.struct.member.value.string            
  from:
    - input.methodCall.params.param.value.array.data.value.struct.member.name.$
    - input.methodCall.params.param.value.array.data.value.struct.member.value.string.$
  values: |- 
    var retValue1 = '';

    if($(input.methodCall.params.param.value.array.data.value.struct.member.name.$) == 'phone'){
      retValue1=input.methodCall.params.param.value.array.data.value.struct.member.value.string.$;
    }

    retValue1;

I appreciate your help !!

1

There are 1 best solutions below

0
Joaquín Figueroa Arrué On

I solve this in two phases of mapping:

two

  1. Create an array called members, where each node is of type member, which has name and value properties. This 'members' array is the receiver of the data coming from the request.

enter image description here

  1. In the second phase of the mapping, I took the output variable from the previous mapping (of type members) and assigned it to message.body. This with the aim of getting rid of the field names with a dollar symbol ($), so the mapping will not give any error for not recognizing it.

enter image description here

enter image description here