GithubHelp home page GithubHelp logo

json-mapper-json's Introduction

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
    formatting: <Function> // optional (ex: function(value) {return value + '_formatted';})
    type: <NativeType> // optional (ex: String, Number, Boolean, ...) (not supported yet)
    nested: { <Object> // optional
      newNestedFieldName: <String>,
      formatting: <Function> // 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.

Example

Basic

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

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) => {return value + '_formatted';},
  },
}).then((result) => {
  /*
  result === [
    {'new_field': 'value1_formatted'},
    {'new_field': 'value2_formatted'},
    {'new_field': 'value3_formatted'},
  ]
  */
});

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) => (`${value._index}/${value._type}`),
			},
		},
	},
}).then((result) => {
/*
  result === {
    'hits': [{
      'id': 123456,
      'type': 'some_index/some_type',
    }],
  }
*/
});

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

json-mapper-json's People

Contributors

marchah avatar slamuu avatar

Stargazers

Gábor Mihálcz avatar

Watchers

Ankit Jain avatar Sparsh Gupta avatar Kushagra Gour avatar James Cloos avatar  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.