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-data-mapper

v1.0.3

Published

Json Mapper Utility to transform Json to required format based on the schema given.

Downloads

295

Readme

Json-data-mapper

Introduction

Single page applications will have JSON responses from the middleware API's. Most of the time we definitely require an adapter layer to transform data from API as needed by the UI layer. This module provides a single adaptive layer which will transform the JSON received from API to the UI required format. This is a native typescript module.

Get Started

npm i json-data-mapper --save

How To Use

Specify the JSON Schema to which the data has to be transformed in a Json file, in the below format

sample.json


{
      isList:true,
      data:'',
      format: {
          "label": {
              sourceField:"name"
          },
          "data": {
              sourceField :"template"
          },
          "transform":{
              sourceField: "(name,template)=>{return name+'%%'+template;}",
              type:"FUNCTION"
          } 
      }
 }
 

Build Schema

  • The isList property tells whether the output Json should be a list.
  • You can set data property to any object from the API response and the library will process the data only in that object specified, omitting all other objects in the API response.
  • The format object contains the actual format in which the data mapping has to be done.
    • Each key represents a property in the output Json.
    • sourceField - should be the key of the value to be processed from the API data. This can also accept a transform fn in which case the parameters to the function should be the keys of the value to be processed from the API data. You can pass a complete function as a string as shown in the sample.
    • type - if the sourceField is provided with a transform function then should be set to FUNCTION. if its a property in output json then should be set to FIELD.

Implementation

In your application, Import the json file and add below code.

import { JsonMapper } from 'json-data-mapper';

const transformedData = JsonMapper.formatToSchema(schema, data);

In the code above schema is the Json schema imported from the json file, and data is your actual API response which has to be transformed.

Using this library you can maintain all your transforms in a single JSON file and let the library do the data mapping for all the API's. if the API layer changes the JSON schema alone has to be tweaked as per needs avoiding any code change in the UI layer.

Example

This is a sample schema

{
   isList:true,    
   format: {
     "label": {
         sourceField:"name"
     },
     "data": {
         sourceField :"template"
     },
     "transform":{
         sourceField: "(name,template)=>{return name+' has '+template;}",
         type:"FUNCTION"
     } 
   }
 }

Input Data

 {
   name: "user",
   template: "test Template"
 }

Transformed Data

 { 
   label: 'user',
   data: 'test Template',
   transform: 'user has test Template' 
 }

Contributor

@gkrthk