Parse multiple JSON files, manipulate their contents, and store the updated data

I'm currently working on implementing this functionality using Gulp.

  1. Locate and access all files with the extension .json within a designated directory, including any subdirectories.
  2. Perform modifications to the files in some manner, such as adding a new top-level element or similar adjustments.
  3. Save the modified files into a separate directory while preserving the original file structure.

The area where I am encountering difficulties is understanding how to properly utilize the read/write JSON piping syntax in conjunction with the src method.

Here is an outline of my current setup:

gulp.task("migratefiles", function () {
  return gulp.src("files/**/*.json")
      .pipe(/* SEEKING SOLUTION HERE */)
      .pipe(gulp.dest("processed"));
});

Answer №1

Here are several approaches you can take to achieve this task:

(1) Utilize the gulp-json-transform plugin:

var jsonTransform = require('gulp-json-transform');

gulp.task("migratefiles", function () {
  return gulp.src("files/**/*.json")
    .pipe(jsonTransform(function(json, file) {
      var transformedJson = {
        "newRootLevel": json
      };
      return transformedJson;
    }))
    .pipe(gulp.dest("processed"));
 });

Pros:

  • Simplicity in usage
  • Supports asynchronous processing (if a Promise is returned)
  • Provides access to the path of each file

Cons:

  • Basic output formatting capabilities only

(2) Employ the gulp-json-editor plugin:

var jeditor = require('gulp-json-editor');

gulp.task("migratefiles", function () {
   return gulp.src("files/**/*.json")
     .pipe(jeditor(function(json) {
       var transformedJson = {
         "newRootLevel": json
       };
       return transformedJson;
     }))
     .pipe(gulp.dest("processed"));
});

Pros:

  • User-friendly interface
  • Detects and matches the indentation used in input files automatically (e.g., two spaces, four spaces, tabs, etc.) for consistent output file formatting
  • Offers support for various js-beautify options

Cons:

  • Seems not to support asynchronous processing
  • Lacks a direct way to access the path of each file

(3) Manual approach (directly accessing the vinyl file object using map-stream):

var map = require('map-stream');

gulp.task("migratefiles", function () {
   return gulp.src("files/**/*.json")
     .pipe(map(function(file, done) {
       var json = JSON.parse(file.contents.toString());
       var transformedJson = {
         "newRootLevel": json
       };
       file.contents = new Buffer(JSON.stringify(transformedJson));
       done(null, file);
     }))
     .pipe(gulp.dest("processed"));
});

Pros:

  • Complete control and access over all components
  • Supports asynchronous processing with a done callback

Cons:

  • Might be more challenging to work with

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

`How can I eliminate all duplicate entries from an array of objects in Angular?`

arr = new Array(); arr.push({place:"1",name:"true"}); arr.push({place:"1",name:"false"}); arr.push({place:"2",name:"false"}); arr.push({place:"2",name:"false"}); arr.push({place:"3",name:"false"}); arr.push({place:"3",name:"true"}); I'm curious about ...

Transforming a map into JSON with Playframework

How do I convert a map (defined in a scala template file) with the following structure: <Integer, Map<Integer, Double>>, to a JSON string? I have attempted to use the following code snippet: @Json.stringify(Json.toJson(moduleId2DecileMap)) H ...

During the installation process of Next JS, I faced a challenge that hindered

While setting up NextJS, I ran into the following issue: D:\Codes\React\Learn>npx create-next-app npm WARN using --force Recommended protections disabled. npm WARN using --force Recommended protections disabled. npm ERR! code E404 npm ERR ...

A guide to successfully interacting with multiple elements simultaneously at a single spot

Within my graphic chart, I have various dots that may be located in the same spot. I am looking for a way to handle clicks on two or more elements simultaneously in Vue 3. Do you know of any straightforward methods to achieve this? I attempted using refs ...

Step-by-step guide to aligning layout using material design in generator-gulp-angular

I am currently using generator-gulp-angular with angular-material-design and ui-router All views are loaded into index.html in the <div ui-view></div> element However, when I attempt to center the element with material design directives insid ...

Having all elements at the same height

I am looking to enhance my JavaScript code to only impact 4 items at a time. The goal is to target the first 4 instances of .prodbox, adjust their height accordingly, then move on to the next set of 4 and repeat the process. This sequential adjustment will ...

NodeJS loop issue with variable scoping in the context of express and mongoose

My Tech Stack: NodeJS, express, mongoose var i; for(i = 0; i < results.length; i++){ console.log("out: "+i); RegionData.findOne({'rid': results[i].region_id}, function (err, product) { if (product) { console.log("i ...

Deciphering the intricacies of the http request

I encountered an issue while trying to send a POST request using jQuery AJAX. Upon making the request, I received the following error: XMLHttpRequest cannot load. Response for preflight has invalid HTTP status code 403 Now, I am unsure if the mistake i ...

Are you facing difficulties while trying to utilize useContext in your React application?

I have a basic React application where I need to implement useContext. (by the way, I am using Vite + React) Below is the code for my Context.jsx file: import React, {useContext} from 'react'; const emailContext = React.createContext(); expor ...

"Mastering the art of event delegation: A guide to effectively engaging with various

I have data that is generated dynamically, as shown in the snippet below: const resultsDiv = document.getElementById("results"); const getList = document.getElementById("example"); document.querySelector(".container").addEventListener("click", function ...

When using express, the response.sendFile() function is causing an Uncaught SyntaxError with the error message: Unexpected token <

Currently, I'm utilizing react-router with browserHistory. There is a specific function in my code that is meant to send the Index.html file since the routing is handled client-side with react-router. However, there seems to be an issue where the serv ...

Upon clicking a table row, generate a div underneath containing mapped data

My dynamic table is currently being filled with rows based on an array, but I want to display more data in an expanded view when a button in a row is clicked. However, I'm facing challenges as it seems I can't have two table rows within the same ...

What is the best way to incorporate CSS into JavaScript code?

Here is the code I am working with: <script type = "text/javascript"> function pic1() { document.getElementById("img").src = "../images/images/1.png"; } function pic2() { document.getEl ...

The Shopify cart.json request has encountered a setback with error 330

My current task involves using jQuery to retrieve cart data from Shopify, which I then display on another website. However, this process has suddenly stopped working. When I attempt to make the request in Google Chrome, it shows as 'failed' and u ...

What is the best way to send a message to multiple clients using different workers in both WebSocket and Express?

Currently in my express app, I am utilizing clusters and the ws package to run my project with a socket connection. The issue arises when a client connects to the socket - only one worker (such as worker with id 1) handles the connection. If another client ...

How to use the Enter key to submit a form in react.js with the help of "@material-ui/core/Button"

Here is the form I have created using the render method for user sign-in. <form onSubmit={this.handleSubmit}> <Avatar className={classes.avatar}> <LockOutlinedIcon /> </Avatar> <Typography component="h1" varia ...

Passing an array of items as a property to a child component in React with Typescript is not possible

In my project, I have multiple classes designed with create-react-app. I am trying to send an array of objects to child components as illustrated below. Items.tsx import * as React from 'react'; import ItemTable from './ItemTable'; imp ...

Obtaining quotations around variables in Flutter JSON: A step-by-step guide

I'm currently utilizing Flutter to transmit JSON headers to my NodeJS server. My objective is to send an email along with a token. However, it seems there's an issue with the code I've implemented. String authuser = '{"email" ...

Safari re-downloads background image when revisiting with jQuery CSS

Using jQuery to set a background-image on my website: $('.header-image').css('background-image', 'url(/img/image.jpg)'); However, upon returning to the page from Safari, the image is downloaded again, unlike Chrome and F ...

Extract and loop through JSON data containing various headers

Having no issues iterating through a simple JSON loop, however, the API I am currently utilizing returns a response with multiple headers. Despite my efforts in various methods to access the results objects, I still face some challenges. The response struc ...