cancel
Showing results for 
Search instead for 
Did you mean: 

Remove json key value from document based on condition

fajosa
New Contributor II

Greetings!

Is there a way we can use the mapper to remove “upperlimit” key value from the document, whenever “ApprovalType”==“Greater than equal to” and ignore it if “ApprovalType”==“Between”??

image

Appreciate your help!!

Thanks,
F.

1 ACCEPTED SOLUTION

Yes, it is possible to map the upperlimit to the lowerlimit, you can try with the following expression:

jsonPath($, "Project[*]").map(val => val.ApporvalType.toLowerCase() == "greater than equal to" ? val.extend({lowerlimit: val.upperlimit}).filter((v, k) => k != "upperlimit") : val)

Here I’m using the extend() method, with this you can add a specific field to the object or overwrite an existing one with a new value. In this case the upperlimit value will be assigned to the lowerlimit field, resulting in overwrite of that same field.

View solution in original post

4 REPLIES 4

j_angelevski
Contributor III

Hi @fajosa,

Yes, you can remove that field, we will need to iterate over the Project field since it is an array, try with the following expression:

jsonPath($, "Project[*]").map(val => val.ApporvalType.toLowerCase() == "greater than equal to" ? val.filter((v, k) => k != "upperlimit") : val)

This will remove the upperlimit field if ApprovalType is “Greater than equal to” otherwise it will ignore it.

fajosa
New Contributor II

That worked!! Thank you @j.angelevski

Also, is it possible we can map the upperlimit to the lowerlimit?

so that whenever, ApprovalType ==“Greater than equal to”, $lowerlimit should have the value of $upperlimit which works along with the filter out of upperlimit.

image

Appreciate your help @j.angelevski 😀

Thanks,
F.

Yes, it is possible to map the upperlimit to the lowerlimit, you can try with the following expression:

jsonPath($, "Project[*]").map(val => val.ApporvalType.toLowerCase() == "greater than equal to" ? val.extend({lowerlimit: val.upperlimit}).filter((v, k) => k != "upperlimit") : val)

Here I’m using the extend() method, with this you can add a specific field to the object or overwrite an existing one with a new value. In this case the upperlimit value will be assigned to the lowerlimit field, resulting in overwrite of that same field.

fajosa
New Contributor II

That worked perfectly! Thank you so much! 😀 @j.angelevski