2017-07-03 169 views
1

我從包含JSON陣列,我試圖刪除除標題,列和searchSourceJSON所有元素的API如下回應:如何從此JSON數組中刪除不需要的元素?

"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\":[\"@[email protected]\"],\"post_tags\":[\"@/[email protected]\"],\"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\":[\"@[email protected]\"],\"post_tags\":[\"@/[email protected]\"],\"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\"}}}}]}" 
      } 
     } 
    } 
] 

有人能幫助我弄清楚如何刪除不需要的元素從這個使用JavaScript的JSON文件?

編輯︰答:謝謝大家的偉大的信息!

+1

你可以只創建一個新的JS對象,並從您想保留 – LiverpoolOwen

+2

如果你只在短短的興趣屬性取值幾個屬性,爲什麼當你只需用你正在尋找的屬性創建一個新的對象時,它們都會從這個大數據結構中剝離出來? 'hits.map(hit =>({title:hit._source.title,/ * ... * /}))' – Will

回答

1

您可以:

  • 橫向所有的結果(往往是昂貴的)採用map的方法。

  • 或者因爲它似乎是一個請求彈性搜索篩選出由ES發送的響應。因此,減少網絡流量,可以避免清理數據的需要。

您可以添加filter path on your query string這樣:

_search?filter_path=hits.hits._source

+1

完美,謝謝! 是的,這是Elasticsearch的迴應。感謝您揭露我的過濾路徑!我真的應該通過所有的文件... –

2

一個低效的方法是使用Array#Map,然後選擇只有你想要的元素。

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\":[\"@[email protected]\"],\"post_tags\":[\"@/[email protected]\"],\"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\":[\"@[email protected]\"],\"post_tags\":[\"@/[email protected]\"],\"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)

0

您可以解析JSON,然後映射到創建包含對象只用屬性的新數組,你需要:

const data = JSON.parse(yourJSON); 
const hits = data.hits.map(hit => ({ 
    title: hit._source.title, 
    searchSourceJSON: hit._source.exampleSavedObjectMeta.searchSourceJSON 
}); 
+0

它似乎已經是一個Object而不是一個字符串。我不認爲JSON.parse是必要的。 –

0
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}); 
}); 

這將刪除所有屬性(垃圾收集它們),除了通緝。

0

試試這個:

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\":[\"@[email protected]\"],\"post_tags\":[\"@/[email protected]\"],\"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\":[\"@[email protected]\"],\"post_tags\":[\"@/[email protected]\"],\"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);