Reading and writing JSON data with streams

JSON (JavaScript Object Notation) is a popular data interchange format used in web development and APIs. When working with large JSON datasets, it could be more efficient to read and write data using streams instead of loading the entire JSON file into memory. In this blog post, we will explore how to read and write JSON data using streams in different programming languages.

Reading JSON using Streams

Streams allow us to process data in chunks, providing a memory-efficient way to handle large JSON files. Let’s look at how we can use streams to read JSON data in our code.

JavaScript (Node.js)

In JavaScript (Node.js), we can use the built-in fs module along with JSONStream package to read JSON data using streams. First, let’s install JSONStream package using npm:

npm install jsonstream

Here’s an example showing how to read JSON data using streams in JavaScript:

const fs = require('fs');
const JSONStream = require('JSONStream');

const jsonStream = fs.createReadStream('data.json');

jsonStream
  .pipe(JSONStream.parse('*'))
  .on('data', (data) => {
    // Process each chunk of JSON data
    console.log(data);
  })
  .on('error', (err) => {
    console.error('Error reading JSON data:', err);
  });

Python

In Python, we can use the json and ijson libraries to read JSON data using streams. First, let’s install ijson library using pip:

pip install ijson

Here’s an example showing how to read JSON data using streams in Python:

import ijson

with open('data.json', 'rb') as jsonFile:
    jsonObjects = ijson.items(jsonFile, 'item')
    for jsonObject in jsonObjects:
        # Process each chunk of JSON data
        print(jsonObject)

Writing JSON using Streams

Now that we know how to read JSON using streams let’s see how we can write JSON data using streams in different languages.

JavaScript (Node.js)

In JavaScript (Node.js), we can use the JSONStream package along with fs module to write JSON data using streams. Here’s an example:

const fs = require('fs');
const JSONStream = require('JSONStream');

const data = [
  { name: 'John', age: 25 },
  { name: 'Jane', age: 30 },
  { name: 'Mike', age: 35 }
];

const jsonStream = JSONStream.stringify();
const outputStream = fs.createWriteStream('output.json');

jsonStream.pipe(outputStream);

data.forEach((item) => {
  jsonStream.write(item);
});

jsonStream.end();

Python

In Python, we can use the json library to write JSON data using streams. Here’s an example:

import json

data = [
  { 'name': 'John', 'age': 25 },
  { 'name': 'Jane', 'age': 30 },
  { 'name': 'Mike', 'age': 35 }
]

with open('output.json', 'w') as jsonFile:
    for item in data:
        json.dump(item, jsonFile)
        jsonFile.write('\n')

Conclusion

Using streams to read and write JSON data can be beneficial when dealing with large datasets, as it provides a memory-efficient way to process the data. In this blog post, we learned how to read and write JSON using streams in JavaScript (Node.js) and Python. By utilizing streams, developers can improve the performance and scalability of their applications dealing with JSON data.

#JSON #streaming