0

I'm trying to filter the json file so that it only contains certain key-value pairs. I have been looking around stack overflow but none of the solutions meet my needs.

I made the json file into a var and tried using this piece of code but it fails for some reason, saying that [object array] is not a function.

var newArr = data.map(data, function(object) {
  return data.pick(object, ['time', 'practical_salinity']);});
[
  {
    "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
  }] 

From this json file, I would like the output to be,

[{"practical_salinity": 33.435064643342436,
  "pressure": 629441}]
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Brian Lee
  • 25
  • 1
  • 3
    You can see how here on [How to get a subset of a javascript object's properties](https://stackoverflow.com/questions/17781472/how-to-get-a-subset-of-a-javascript-objects-properties) - the same concepts can be applied within your map function. – smashed-potatoes May 07 '19 at 22:17

2 Answers2

0

Like smashed-potatoes mentioned, this is mostly of duplicate of How to get a subset of a javascript object's properties. You just need to wrap it correctly in the Array.map function.

map takes the iteratee as the first argument. I don't know what object.pick is supposed to be.

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
   }))
);
Doug Coburn
  • 2,485
  • 27
  • 24
0

The JSON.parse reviver parameter can be used to exclude key-value pairs from JSON string :

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 );
Slai
  • 22,144
  • 5
  • 45
  • 53