0

I need to get the value at a given index in a json array containing millions of entries.

The json file looks a bit like this:

[
    {"street":"123 Here street", "city":"Here city"},
    {"street":"456 There street", "city":"There city"}
]

I can get away with it using stream-json with the following code:

const { chain } = require('stream-chain');
const { parser } = require('stream-json');
const { streamArray } = require('stream-json/streamers/StreamArray');

const fs = require('fs');

const chosenIndex = 8
const pipeline = chain([
    fs.createReadStream(filepath),
    parser(),
    streamArray()
]);

pipeline.on('data', data => {
    if (data.key == chosenIndex) {
        console.log(data.value);
    }
});

pipeline.on('end', () => { console.log('All Done'); });

I'm just afraid this is not the fastest way to do this. Thanx in advance

0 Answers0