过滤json文件,使其仅具有某些键值对

时间:2019-05-07 22:11:45

标签: javascript json

我正在尝试过滤json文件,以使其仅包含某些键值对。我一直在寻找堆栈溢出的问题,但是没有一个解决方案可以满足我的需求。

我将json文件制成了var,并尝试使用这段代码,但是由于某种原因它失败了,并说[object array]不是一个函数。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

capabilities = {
     "browserName": "firefox"}

profile = webdriver.FirefoxProfile()
profile.add_extension('/Users/cookie_manager.xpi')

driver = Remote(browser_profile=profile,
     command_executor="http://url.com:4444/wd/hub",
     desired_capabilities=capabilities)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

从这个json文件中,我希望输出为

var newArr = data.map(data, function(object) {
  return data.pick(object, ['time', 'practical_salinity']);});

2 个答案:

答案 0 :(得分:0)

就像提到的土豆一样,这大部分是How to get a subset of a javascript object's properties的副本。您只需要将其正确包装在Array.map函数中即可。

map将iteratee作为第一个参数。我不知道object.pick应该是什么。

const data = [
  {
    "practical_salinity": 33.435064643342436,
    "seawater_pressure": 101.78123944323164,
    "corrected_dissolved_oxygen": 164.09190464800648,
    "density_qc_executed": 29,
    "driver_timestamp": 3765193211.34625,
    "conductivity": 1493552,
    "seawater_pressure_qc_results": 29,
    "practical_salinity_qc_results": 29,
    "temperature": 411414,
    "density": 1026.3321779687496,
    "corrected_dissolved_oxygen_qc_executed": 29,
    "corrected_dissolved_oxygen_qc_results": 29,
    "seawater_temperature_qc_results": 29,
    "pressure_temp": 14964,
    "internal_timestamp": 0.0,
    "seawater_conductivity_qc_results": 13,
    "pk": {
      "node": "SF01A",
      "stream": "ctdpf_sbe43_sample",
      "subsite": "RS01SBPS",
      "deployment": 6,
      "time": 3765193211.283541,
      "sensor": "2A-CTDPFA102",
      "method": "streamed"
    },
    "ext_volt0": 22775,
    "seawater_temperature": 9.178755142917169,
    "ingestion_timestamp": 3765193215.771,
    "port_timestamp": 3765193211.283541,
    "seawater_pressure_qc_executed": 29,
    "pressure": 629441,
    "preferred_timestamp": "port_timestamp",
    "seawater_conductivity": 3.5856973775744,
    "practical_salinity_qc_executed": 29,
    "seawater_temperature_qc_executed": 29,
    "density_qc_results": 29,
    "time": 3765193211.283541,
    "seawater_conductivity_qc_executed": 29
  }];
  
console.log(data.map(({ time, practical_salinity }) => ({
    time, practical_salinity
   }))
);

答案 1 :(得分:0)

JSON.parse reviver parameter可用于从JSON字符串中排除键/值对:

var keys = ['pressure', 'practical_salinity'], json = '[{"practical_salinity":33.435064643342436,"seawater_pressure":101.78123944323164,"corrected_dissolved_oxygen":164.09190464800648,"density_qc_executed":29,"driver_timestamp":3765193211.34625,"conductivity":1493552,"seawater_pressure_qc_results":29,"practical_salinity_qc_results":29,"temperature":411414,"density":1026.3321779687496,"corrected_dissolved_oxygen_qc_executed":29,"corrected_dissolved_oxygen_qc_results":29,"seawater_temperature_qc_results":29,"pressure_temp":14964,"internal_timestamp":0,"seawater_conductivity_qc_results":13,"pk":{"node":"SF01A","stream":"ctdpf_sbe43_sample","subsite":"RS01SBPS","deployment":6,"time":3765193211.283541,"sensor":"2A-CTDPFA102","method":"streamed"},"ext_volt0":22775,"seawater_temperature":9.178755142917169,"ingestion_timestamp":3765193215.771,"port_timestamp":3765193211.283541,"seawater_pressure_qc_executed":29,"pressure":629441,"preferred_timestamp":"port_timestamp","seawater_conductivity":3.5856973775744,"practical_salinity_qc_executed":29,"seawater_temperature_qc_executed":29,"density_qc_results":29,"time":3765193211.283541,"seawater_conductivity_qc_executed":29}]';

var result = JSON.parse(json, (k, v) => keys.includes(k) || !isNaN(k) ? v : void 0);

console.log( result );