npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-mapper-json

v1.3.3

Published

convert json object into a new formatted json object

Downloads

5,641

Readme

json-mapper-json

Simple library to convert a json object into a new json object formatted by a template.

Installation

Install via npm:

$ npm install json-mapper-json

Documentation

Usage

const jsonMapper = require('json-mapper-json');

jsonMapper(json<Object>, template<Object>) => Promise

Template Syntax Explanations

{
  newFieldName1: {
    path: <String>, // required
    required: <Boolean> // not required, default `true`
    formatting: <Function> // optional (ex: function(value) {return value + '_formatted';})
    defaultValue: <AnyType> // optional
    type: <NativeType> // optional (ex: String, Number, Boolean, ...) (not supported yet)
    nested: { <Object> // optional
      newNestedFieldName: <String>,
      required: <Boolean> // not required, default `true`
      formatting: <Function> // optional
      defaultValue: <AnyType> // optional
      type: <NativeType> // optional (ex: String, Number, Boolean, ...) (not supported yet)
      nested: { <Object> // optional
        ...
      },
    },
  },
  newFieldName2: <String> // (it's the path, syntactic sugar for {path: ''})
  ...
}

Path Key Words

  • $root: give possibility to access the root given object in a nested path.
  • $item: give possibility to access of the all item of an array in a nested path.
  • $empty: give possibility to create skip path, to create empty object to be able to merge paths (see example).

Example

Basic

jsonMapper({
  field: 'value',
}, {
  'new_field': {
    path: 'field',
  },
}).then((result) => {
  /*
  result === {
    'new_field': 'value',
  }
  */
});

Basic with required

jsonMapper({
  field1: 'value1',
}, {
  'new_field1': {
    path: 'field1',
  },
  'new_field2': {
    path: 'field2',
    required: false,
  },
}).then((result) => {
  /*
  result === {
    'new_field1': 'value1',
  }
  */
});

Basic with defaultValue

jsonMapper({
  field1: 'value1',
}, {
  'new_field1': {
    path: 'field1',
  },
  'new_field2': {
    path: 'field2',
    defaultValue: 'default_value',
  },
}).then((result) => {
  /*
  result === {
    'new_field1': 'value1',
    'new_field2': 'default_value',
  }
  */
});
jsonMapper({
  field1: 'value1',
  nested: {
    field3: 'value3',  
  },
}, {
  'new_field1': {
    path: 'field1',
  },
  'new_field2': {
    path: 'field2',
  },
}).then((result) => {
  /*
  throw Error: Invalid path nested.field2 (field2)
  */
});

Basic with nested

jsonMapper({
  field1: {
    field2: {
      field3: 'value',
      field4: 'value4',
    },
  },
}, {
  'new_field': {
    path: 'field1.field2',
    nested: {
      'nested_field1': {
        path: 'field3',
      },
      'nested_field2': {
        path: 'field4',
      },
    },
  },
}).then((result) => {
  /*
  result === {
    'new_field': {
      'nested_field1': 'value',
      'nested_field2': 'value4',
    }
  }
  */
});

Basic with formatting

jsonMapper({
  field1: {
    field2: {
      field3: 'value',
    },
  },
}, {
  'new_field': {
    path: 'field1.field2.field3',
    formatting: (value) => {return value + '_formatted';},
  },
}).then((result) => {
  /*
  result === {
    'new_field': 'value_formatted',
  }
  */
});

Array

jsonMapper([{
  field: 'value1',
}, {
  field: 'value2',
}, {
  field: 'value3',
},
], {
  'new_field': {
    path: 'field',
  },
}).then((result) => {
  /*
  result === [
    {'new_field': 'value1'},
    {'new_field': 'value2'},
    {'new_field': 'value3'},
  ]
  */
});

Array with formatting

jsonMapper([{
  field: 'value1',
}, {
  field: 'value2',
}, {
  field: 'value3',
},
], {
  'new_field': {
    path: 'field',
    formatting: (value, index) => (`${value}_formatted_${index}`),
  },
}).then((result) => {
  /*
  result === [
    {'new_field': 'value1_formatted_0'},
    {'new_field': 'value2_formatted_1'},
    {'new_field': 'value3_formatted_2'},
  ]
  */
});

Usage of the syntactic sugar for path

jsonMapper({
  field: 'value',
}, {
  'new_field': 'field',
}).then((result) => {
  /*
  result === {
    'new_field': 'value',
  }
  */
});

Array with nested and path syntactic sugar

jsonMapper([{
  field: {'nested_field': 'value1'},
}, {
  field: {'nested_field': 'value2'},
}, {
  field: {'nested_field': 'value3'},
},
], {
  'new_field': {
    path: 'field',
    nested: {
      'new_nested_field': 'nested_field',
    },
  },
}).then((result) => {
  /*
  result === [
    {'new_field': {'new_nested_field': 'value1'}},
    {'new_field': {'new_nested_field': 'value2'}},
    {'new_field': {'new_nested_field': 'value3'}},
  ]
  */
});

Usage of the key word $root for path

jsonMapper({
  'content': {
    'result': [
      {
        'courseStatisticsDto': {
          'times': 3,
          'persons': 1,
          'courseCode': '',
        },
        'courseAddressDto': {},
        'endDate': 1460590552000,
        'startDate': 1460590552000,
        'name': 'Example Course',
      },
    ],
    'type': 'offline',
  },
}, {
  data: {
    path: 'content.result',
    nested: {
      name: 'name',
      code: 'courseStatisticsDto.courseCode',
      type: '$root.content.type',
    },
  },
}).then((result) => {
/*
  result === {
    'data': [{
      'name': 'Example Course',
      'code': '',
      'type': 'offline',
    }],
  }
*/
});

Usage of the key word $item for path

jsonMapper({
	hits: {
		total: 1,
		hits: [{
			_index: 'some_index',
			_type: 'some_type',
			_id: '123456',
			_score: 1,
			_source: {
				id: 123456
			},
		}],
	},
}, {
	hits: {
		path: 'hits.hits',
		nested: {
			id: '_source.id',
			type: {
				path: '$item',
				formatting: (value, index) => (`${value._index}/${value._type}/${index}`),
			},
		},
	},
}).then((result) => {
/*
  result === {
    'hits': [{
      'id': 123456,
      'type': 'some_index/some_type/0',
    }],
  }
*/
});

Usage of the key word $empty for path

jsonMapper({
  field1: {
    field2: {
      field3: 0
    },
    field4: {
      field5: ['value1', 'value2'],
    },
  },
}, {
  'new_field1': {
    path: 'field1',
    nested: {
      'new_field2': {
        path: '$empty',
        nested: {
          new_field3: {
            path: 'field2.field3'
          },
          new_field4: {
            path: 'field4.field5',
          },
        },
      },
    },
  },
}).then((result) => {
  /*
  {
    new_field1: {
      new_field2: {
        new_field3: 0,
        new_field4: ['value1', 'value2'],
      },
    },
  }
  */
});

## Note

this library is very usefull when you have well design models and have to communicate with horrible webservices.

## TODO

* manage `type` property

## Contributing

This project is a work in progress and subject to API changes, please feel free to contribute