The structure of json file is as follows:
{
"products": [
{
"id": 6672129786814,
"title": "rhPQbUW2fK5bhVCFuNFPsBGdolZYYcJ9gp4D4gskBHCOmGWb54",
"variants":[{...},{...}],
....
},{
"id": 6672129786824,
"title": "yuhPQbUW2fK5bhVCFfgdfgdfsglZYYcJ9gp4D4gskBHCOmGWb54",
"variants":[{...},{...}],
....
},{
"id": 6672129786842,
"title": "dfgUW2fK5bhVCFuNfgdsfgolZYYcJ9gp4D4gskBHCOmGWb54",
"variants":[{...},{...}],
....
},{
"id": 6672129786935,
"title": "aayuy44fK5bhVCFuNFPsBGdolZYYcJ9gp4D4gskBHCOmGWb54",
"variants":[{...},{...}],
....
}]
}
In this json file there can be 500000 number of objects. I need to search product basis on the product_id
.
I know that we can read file using streaming, and it is also working fine. it gives me result as all objects of json file. But Now I need to search specific product basis on the product_id.
Here I know that during getting all products and I can use the loop iteration and search the specific product. but I don't think that it is a efficient way to search.
I am looking for solution to search the specific object basis on the value of id, during the reading of file so that I can search quickly and get that specific object, rather than get all objects once and then iterate and then match the id and get that object.
var data = ''
var reader_stream = fs.createReadStream(file_path) //Create a readable stream
reader_stream.setEncoding('UTF8')
reader_stream.on('data', function(chunk) {
data += chunk
})
reader_stream.on('end',function() {
const products = JSON.parse(data)
resolve(products)
})
reader_stream.on('error', function(err) {
console.log(err.stack)
reject(err.stack)
})
If I use chunk, it is not sure it consider the complete object from objects [as it is chunk]. so How should I read data object by object? Can Anyone please provide the solution using which I can get that specific object quickly?