1.9.0 • Published 12 months ago

replace-json-property v1.9.0

Weekly downloads
1,775
License
MIT
Repository
github
Last release
12 months ago

code style: prettier codecov semantic-release

Logo

This module allows you to replace a specific property in a JSON.

Table of Contents generated with DocToc

Comand line usage

You can use this via the following command:

replace-json-property pathToFile property value

This command replaces all occurences of the matching property. It also replaces occurences in nested objects or objects in arrays.

The following command would replace all values of the foo property with 'test' inside the test.json.

replace-json-property ./test.json foo test,

Executing the command above on the given JSON

{
    "foo": "bar",
    "a": {
        "b": 1,
        "foo": "bar",
        "c": [
            {"d": 1, "foo": "bar"},
            {"d": 2, "foo": "bar"},
            {"d": 3, "foo": "bar"},
        ]
    }
}

results in:

{
    "foo": "test",
   	"a": {
        "b": 1,
        "foo": "test",
        "c": [
            {"d": 1, "foo": "test"},
            {"d": 2, "foo": "test"},
            {"d": 3, "foo": "test"},
        ]
    }
}

Short form

All commands explained above can also be run with the shortcut version rjp.

rjp ./test.json foo test,

Usage from code

You can also use the replace function in your JavaScript code.

  1. Using module imports

    import {replace} from 'replace-json-property';
    
    replace('./environment/test.json', 'foo', 'new value');
  2. Using commonjs

    const replaceJSONProperty = require('replace-json-property');
    
    replaceJSONProperty.replace('./environment/test.json', 'foo', 'new value');

Options

The following flags allow you to configure how the resulting file is written.

OptionDescriptionDefault
-s or --spacesAdd the spaces the file should be written with, for example (2, 4)2
-e or --eolAdd the line ending the file should be written with, for example "\r\n""\n"
--silentSilent mode. Executes without log messagesFalse
--limitLimit the number of replacements0 (unlimited)
--addWhen set to true propertys that do not exist will be addedfalse

Help command

You can always run the help command to see how the signature looks

replace-json-property -h

or

replace-json-property --help

To get the current version use the --version or -v command.

1.9.0

12 months ago

1.8.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago