Search code examples
wso2datamapper

DataMapper mediator: mapping failed when property's parent in input JSON is null


I'm trying to map a result json object with nested fields in wso2 through the data mapper in wso2 esb integrator. Here is what I'm trying to achieve:

Input json file to map:

{
  "name":"John",
  "location": {
    "id": 1,
    "city": "Sydney"
  }
}

Output json file to get:

{
  "name":"John",
  "city": "Sydney"
}

It works fine until the input Json become

{
  "name":"John",
  "location": null
  }
}

the result I need is

{
  "name":"John"
}

but instead I got an exception because location is null.

ERROR {org.wso2.carbon.mediator.datamapper.DataMapperMediator} - DataMapper mediator : mapping failed Error while reading input stream. Script engine unable to execute the script javax.script.ScriptException: TypeError: Cannot get property "city" of null in <eval> at line number 1

My problem is how to handle it properly in DataMapper mediator that field should not be mapped under certain conditions.

If anyone could help me a i would stay quite grateful.

Thank you.


Solution

  • It seems I fixed the problem.

    you can add any condition check in .dmc file in Registry project.

    if (inputroot.location != null) {
        outputroot[0].city = inputroot.location.city;
    }