Skip to content

Instantly share code, notes, and snippets.

@samuelastech
Last active April 4, 2023 12:19
Show Gist options
  • Save samuelastech/633ec05bf5202d63b8da05be4c63a3a5 to your computer and use it in GitHub Desktop.
Save samuelastech/633ec05bf5202d63b8da05be4c63a3a5 to your computer and use it in GitHub Desktop.
A script that cleans up OCI `products.json`
import { createReadStream, createWriteStream, writeFile } from 'node:fs';
import { Transform, Writable } from 'node:stream';
import JSONStream from 'JSONStream';
import axios from 'axios';
// const readStream = createReadStream('./products.json', { encoding: 'utf8' });
let jsonString = '';
const url = 'https://apexapps.oracle.com/pls/apex/cetools/api/v1/products/?currencyCode=BRL';
/**
* Download the "products.json"
*/
const response = await axios({ url, responseType: 'stream', method: 'GET' });
const productStream = response.data.pipe(createWriteStream('products.json'));
productStream.on('finish', () => processFile());
function processFile() {
const readStream = createReadStream('./products.json', { encoding: 'utf8' });
readStream
.pipe(JSONStream.parse('items.*')) // Chunk to the size of each element in the array
.pipe(new Transform({
objectMode: true,
transform(chunk, _, callback) {
chunk.prices = chunk.currencyCodeLocalizations[0].prices;
delete chunk.currencyCodeLocalizations;
callback(null, JSON.stringify(chunk));
},
}))
.pipe(new Writable({
write(chunk, _, callback) {
const item = JSON.parse(chunk);
jsonString += ',' + JSON.stringify(item);
callback();
},
final(callback) {
const json = `{ "items": [${jsonString.slice(1, jsonString.length)}] }`;
writeFile('productsBRL.json', json, (err) => {
console.error(err);
});
callback();
},
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment