Skip to content

Latest commit

 

History

History
109 lines (85 loc) · 1.84 KB

README.md

File metadata and controls

109 lines (85 loc) · 1.84 KB

Transform JSON

Simple transform JSON object structure. Rename JSON keys with ease.

Why?

First goal of this node module was to modify and maintain the structure of many translation files of angular-translate. Please note that the values of the input files never change, but the structural information.

Use it in your node project

npm install transform-json --save-dev
var transformJson = require('transform-json');

var input = {
  a: 'some string'
};
var transform = {
  'a': 'b' // renames 'a' to 'b'
};
var output = transformJson(input, transform);
console.log('output: ' + JSON.stringify(output)); // output: {"b":"some string"}

Use it with CLI and external JSON files

npm install -g transform-json
transform-json-cli --input=input.json --transform=transform.json --output=output.json

Example files

input.json:

{
  "TRANSLATE_THIS": "My Translation",
  "NAME": "My Name",
  "SETTINGS": {
    "SECTION_1": {
      "MOO": "YES"
    }
  }
}

transform.json:

{
  "NAME": ["SETTINGS.SECTION_2.MOO", "NAME"],
  "TRANSLATE_THIS": "TRANSLATION"
}

output.json:

{
  "TRANSLATION": "My Translation",
  "NAME": "My Name",
  "SETTINGS": {
    "SECTION_1": {
      "MOO": "YES"
    },
    "SECTION_2": {
      "MOO": "My Name"
    }
  }
}

Transformations

  • Basically, a transform describes
{
  "Let's rename this key": "into this key"
}
  • You can also give an array as value
{
  "Let's rename this key": ["into this key", "and this key"]
}
  • You also can look into deep objects
{
  "PARTS.PART1": "PARTS.PART2"
}

(This works also if you don't have a hierarchy but a key which is exactly "PARTS.PART1")

Licence

Copyright (c) 2015 Johannes Herrnegger. Licensed under the MIT license.