cancel
Showing results for 
Search instead for 
Did you mean: 

Remove json object conditionally

acesario
Contributor II

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”
}
]
}]

1 ACCEPTED SOLUTION

cjhoward18
Employee
Employee

@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.

View solution in original post

1 REPLY 1

cjhoward18
Employee
Employee

@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.