walkerline117
8 years agoContributor
Show only partial body of a json
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,
}
}
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$paramsobject, 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)