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