Giter VIP home page Giter VIP logo

json-array-modifier's Introduction

JSON Array Modifier

A package to add additional properties to every object in a JSON array and return a new JSON file or string.


This started out as a single script and I saw the potential to flesh it out and break it apart to use as my first NPM package.

There are zero dependencies here, it is just a series of JS files that take in your original data, alongside an object containing the desired keys and values you want to add to each object in your initial array.

There are examples of the JSON data that the function expects under the /src/lib/mocks dir.

Documentation

Syntax

On Client

const [modifyJsonAndReturnJson, modifyJsonAndReturnFile] = modifyJsonArray();
const newJsonArray = modifyJsonAndReturnJson(input, additions, options);

On Server (NodeJs)

modifyJsonAndReturnFile(input, additions, options);

Parameters

input

This defines the resource of which you wish to modify. This can either be

  • A string path to a JSON file
  • An Array of objects

additions

An object containing the keys and values what you wish added to each object.

options (optional)

An object containing the following options:

random (optional)

A Boolean flag that lets you decide if you want to randomise what value is added to each object per property. Default is set to false.

filename (optional)

The name of the file you'd like to create.

fileSaveLocation (optional)

If you wish to create a file, where would you want the file to be created. Default is ./.

Return value

The return value is dependent on which function you invoke.

  • modifyJsonAndReturnJson - String of json data.
  • modifyJsonAndReturnFile - void. A new file will be created.

Example

This package is only built for use with ESM compatiable projects. Therefore use ESM syntax:

import modifyJsonArray from "json-array-modifier";
import input from "./mocks/mockInput.json" assert { type: "json" };
import additions from "./mocks/mockAdditions.json" assert { type: "json" };

const [modifyJsonAndReturnJson] = modifyJsonArray();

const options = {
  random: true,
  fileName: "newJsonData",
  fileSaveLocation: "../../tmp",
};

const newJSON = modifyJsonAndReturnJson(input, additions, options);

console.log(newJSON);

json-array-modifier's People

Contributors

daniel-workman avatar dwbra avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.