Forum Discussion

acesario's avatar
acesario
Contributor II
5 years ago
Solved

Remove json object conditionally

I have the following document with question responses. When the response is “-”, I would like to remove the question and answer text. Is there a way to do this in a mapper or conditional step?

Before:
[{
“Questions”: [ {
“answerText”: “John”,
“Question_ID”: “FIRST_NAME”
},
{
** “answerText”: “-”,**
** “Question_ID”: “ADDRESS”**
** },**
{
“answerText”: “888-888-8888”,
“Question_ID”: “PHONE”
}
]
}]
After
[{
“Questions”: [ {
“answerText”: “John”,
“Question_ID”: “FIRST_NAME”
},
{
“answerText”: “888-888-8888”,
“Question_ID”: “PHONE”
}
]
}]

  • @acesario

    I used a JSON generator snap with your array of objects at the key Questions and a mapper to filter the array with this expression in a mapper:

    $Questions.filter(value => value.get("answerText") != "-")

    This will remove all objects with answerText == ‘-’.

    This relies on the objects having the key answerText if that is not the case some additional logic can be added if needed.

1 Reply

  • @acesario

    I used a JSON generator snap with your array of objects at the key Questions and a mapper to filter the array with this expression in a mapper:

    $Questions.filter(value => value.get("answerText") != "-")

    This will remove all objects with answerText == ‘-’.

    This relies on the objects having the key answerText if that is not the case some additional logic can be added if needed.