cancel
Showing results for 
Search instead for 
Did you mean: 

Show only partial body of a json

walkerline117
Contributor

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,
}
}

1 ACCEPTED SOLUTION

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)

View solution in original post

3 REPLIES 3

walkerline117
Contributor

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.

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)

Thanks for your solution