Reputation: 582
I have the following response from an API containing a JSON array and I am trying to remove all elements except for title, column, and searchSourceJSON:
"hits": [
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Integration-Enabled",
"_score": 1,
"_source": {
"title": "demo Media Integration - Enabled",
"description": "",
"hits": 0,
"columns": [
"_source"
],
"sort": [
"timestamp",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"value\":\"Media Integration\",\"disabled\":false},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Integration\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Action\",\"value\":\"Enable\",\"disabled\":false},\"query\":{\"match\":{\"Action\":{\"query\":\"Enable\",\"type\":\"phrase\"}}}}],\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}}}"
}
}
},
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Import-True",
"_score": 1,
"_source": {
"title": "demo Media Import - True",
"description": "",
"hits": 0,
"columns": [
"FormFactor",
"_type",
"Identity"
],
"sort": [
"_type",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"*\"}},\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"disabled\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"negate\":false,\"value\":\"Media Import\"},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Import\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Successful\",\"value\":\"True\",\"disabled\":false},\"query\":{\"match\":{\"Successful\":{\"query\":\"True\",\"type\":\"phrase\"}}}}]}"
}
}
}
]
Could someone help me figure out how to delete the unwanted elements from this JSON document using JavaScript?
EDIT: Answered, thank you everyone for the great information!
Upvotes: 2
Views: 1008
Reputation: 27232
Try this :
var jsonObj = [
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Integration-Enabled",
"_score": 1,
"_source": {
"title": "demo Media Integration - Enabled",
"description": "",
"hits": 0,
"columns": [
"_source"
],
"sort": [
"timestamp",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"value\":\"Media Integration\",\"disabled\":false},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Integration\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Action\",\"value\":\"Enable\",\"disabled\":false},\"query\":{\"match\":{\"Action\":{\"query\":\"Enable\",\"type\":\"phrase\"}}}}],\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}}}"
}
}
},
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Import-True",
"_score": 1,
"_source": {
"title": "demo Media Import - True",
"description": "",
"hits": 0,
"columns": [
"FormFactor",
"_type",
"Identity"
],
"sort": [
"_type",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"*\"}},\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"disabled\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"negate\":false,\"value\":\"Media Import\"},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Import\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Successful\",\"value\":\"True\",\"disabled\":false},\"query\":{\"match\":{\"Successful\":{\"query\":\"True\",\"type\":\"phrase\"}}}}]}"
}
}
}
];
var res = jsonObj.map(function(item) {
return {
"title": item._source.title,
"columns": item._source.columns,
"searchSourceJSON": item._source.exampleSavedObjectMeta.searchSourceJSON
};
});
console.log(res);
Upvotes: 0
Reputation: 8954
You can either:
Transverse all the result (which tend to be costly) using a map approach.
Or as it seems to be a request to Elastic Search to filter out the response send by ES. Therefore, less network traffic and you could avoid the need to clean up the data.
You can add a filter path on your query string this way:
_search?filter_path=hits.hits._source
Upvotes: 1
Reputation: 138537
var result=(function(){//IIFE to enshure garbage collection
var hits=[...];//your data
return hits.map(hit=>{hit["_source"].title,hit["_source"].colums,hit.exampleSavedObjectMeta.searchSourceJSON});
});
This deletes all properties (garbage collects them) except the wanted.
Upvotes: 0
Reputation: 1115
You can parse the JSON, and then map to create a new array containing objects with just the properties you need :
const data = JSON.parse(yourJSON);
const hits = data.hits.map(hit => ({
title: hit._source.title,
searchSourceJSON: hit._source.exampleSavedObjectMeta.searchSourceJSON
});
Upvotes: 0
Reputation: 3863
One low effort way is to use Array#Map
and then select only the elements you want.
var hits = [
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Integration-Enabled",
"_score": 1,
"_source": {
"title": "demo Media Integration - Enabled",
"description": "",
"hits": 0,
"columns": [
"_source"
],
"sort": [
"timestamp",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"value\":\"Media Integration\",\"disabled\":false},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Integration\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Action\",\"value\":\"Enable\",\"disabled\":false},\"query\":{\"match\":{\"Action\":{\"query\":\"Enable\",\"type\":\"phrase\"}}}}],\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}}}"
}
}
},
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Import-True",
"_score": 1,
"_source": {
"title": "demo Media Import - True",
"description": "",
"hits": 0,
"columns": [
"FormFactor",
"_type",
"Identity"
],
"sort": [
"_type",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"*\"}},\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"disabled\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"negate\":false,\"value\":\"Media Import\"},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Import\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Successful\",\"value\":\"True\",\"disabled\":false},\"query\":{\"match\":{\"Successful\":{\"query\":\"True\",\"type\":\"phrase\"}}}}]}"
}
}
}
]
var filtered = hits.map(function(hit){
return { title : hit._source.title, searchSourceJSON : hit._source.exampleSavedObjectMeta.searchSourceJSON, columns : hit._source.columns}
})
console.log(filtered)
Upvotes: 3