简体   繁体   English

如何从客户端JS文件中的数组中的多个对象中提取所需的键值对?

[英]How to Extract Desired Key-Value pairs from Multiple Objects in Array in JS File on Client-Side?

I am required to extract desired key-values from a large .js file essentially containing an array of objects with fixed defined keys in json format by uploading it on the client-side. 我需要通过从客户端上载从大型.js文件中提取所需的键值,该文件本质上包含具有json格式的固定定义键的对象数组。

I want to extract desired keys and values from each object. 我想从每个对象中提取所需的键和值。 But the problem is that the file is 2+ GB in size and can't be uploaded all at once. 但问题在于该文件的大小为2+ GB ,无法一次全部上传。 So I need to at best split the file into small chunks and extract the required keys' values. 因此,我最多需要将文件分成小块,然后提取所需键的值。

The .js file looks like the following, and let say I want to extract only b and d keys' values from each object. .js文件如下所示,假设我只想从每个对象中提取b键和d键的值。

var data = [{
  "a": "...",
  "b": [{
    "b-1": "...",
    "b-2": 3,
    "b-3": {"b-3-1": "..."}
  }, {...}, {...}],
  "c": false,
  "d": 25,
  ...
}, ..., {...}] 

I've read about FileReader and Blob Web APIs but don't know which one to use and how so in such a way that when splitting it into small chunks each chunk carries an entire object. 我已经阅读了FileReaderBlob Web API,但不知道使用哪个API,以及如何将API拆分为小块时,每个块都携带一个整个对象。

Please kindly help me on this problem. 请在这个问题上帮助我。

What you need is a streaming JSON parser for Node, eg 您需要的是用于Node的流JSON解析器,例如

If this is a one-of-job you may want to look into jq , (it's like sed for JSON data - you can use it to slice and filter data) eg piping data in and out of it (or you could automate it as well) 如果这是一项工作,则您可能需要研究jq (就像sed一样用于JSON数据-您可以使用它来切片和过滤数据),例如,将数据以管道方式传入和传出(或者可以将其自动化为好)

cat file.json | jq '.a' | less

See: https://shapeshed.com/jq-json/ 参见: https : //shapeshed.com/jq-json/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM