10-15-2018 11:08 AM
Hi
If I have below json body, How would I hide certain fields inside the json body and only show partial of it.
Input:
{
"apiVersion": "1.0",
"noOfRecords": 41607,
"params": {
"q": "*",
"offset": 1,
"limit": 10,
"qt": "na_documents_dismax",
"fq": "archived_flag:false",
"sort": "score desc",
"facetSort": null,
"indent": null,
"wt": "json",
"status": 0,
"version": 2.2,
"queryTime": 7
}
}
Output:
{
“apiVersion”: “1.0”,
“noOfRecords”: 41607,
“params”: {
“q”: “*”,
“offset”: 1,
}
}
Solved! Go to Solution.
10-15-2018 01:09 PM
There’s a filter()
method on objects that you can use to filter properties in objects. For example, to retain only the ‘q’ and ‘qt’ properties in the $params
object, you can use the following expression:
$params.filter((value, key) => ['q', 'qt'].indexOf(key) != -1)
Since the property names are user-supplied, you’d want to replace the ['q', 'qt']
array literal with array from the user. For example, if the names were in a JSON-encoded array in the “field” pipeline parameter, you would use:
$params.filter((value, key) => JSON.parse(_fields).indexOf(key) != -1)
10-15-2018 11:41 AM
Just to add, I know mapper can do this, but we want to have a dynamic filter on this. user will have the ability to indicate which fields they want to see in the response. So I guess somthing in the mapper would make more sense.
10-15-2018 01:09 PM
There’s a filter()
method on objects that you can use to filter properties in objects. For example, to retain only the ‘q’ and ‘qt’ properties in the $params
object, you can use the following expression:
$params.filter((value, key) => ['q', 'qt'].indexOf(key) != -1)
Since the property names are user-supplied, you’d want to replace the ['q', 'qt']
array literal with array from the user. For example, if the names were in a JSON-encoded array in the “field” pipeline parameter, you would use:
$params.filter((value, key) => JSON.parse(_fields).indexOf(key) != -1)
10-15-2018 01:48 PM
Thanks for your solution