Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-unpacker

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-unpacker

A JSON to JSON mapper in TypeScript

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-40%
Maintainers
1
Weekly downloads
 
Created
Source

TypeScript Object Unpacker

This program takes a JSON object and convertes it to a new JSON object using a mapping specification, also written as JSON.

There are some examples of how to use the mapper in the test directory, and a simple example follows:


// Import the Schema Mapper type
import { createSchemaMapper } from './SchemaMapper';

// Create the schema mapper
const mapper = createSchemaMapper();

// This is the data to be transformed
const data = {
  "x": [
    {
      "a": [
        "testa",
        "testb"
      ],
      "au": "%system.metadata.author"
    }
  ]
};

// This is the transformation specification
const mapperData = {
  "x": {
    "rewriteKey": "expanded"
  },
  "a": {
    "rewriteKey": "another",
    "assignKeys": [
      "s",
      "t",
      "u"
    ],
    "rewriteValue": {
      "first": "%s",
      "second": "%t",
      "third": "%u",
      "fourth": "%/subs.system.metadata.name"
    }
  },
  "au": {
    "rewriteKey": "author"
  }
};

// Entries in this object can be referenced by the `data` or the `mapperData`.
// E.g. `%system.metadata.name` and '%system.metadata.author'
const refs = {
  system: {
    metadata: {
      name: 'The System Name',
      author: 'A. Programmer',
    },
  },
};

// Apply the mapping to the data to get an `expanded` object
const expanded: object = mapper.convert(refs, data, mapperData);

// Pretty-print the resulting JSON.
console.log(JSON.stringify(expanded, null, 2));

Running the example code

  1. Run npm install
  2. Create scratch.ts in the src directory.
  3. Compile it using the tsc command
  4. Run it using node dist/scratch.js
  5. Or combine the previous two steps as tsc && node dist/scratch.js

The output should look like this:

{
  "expanded": [
    {
      "another": {
        "first": "testa",
        "second": "testb",
        "fourth": "The System Name"
      },
      "author": "A. Programmer"
    }
  ]
}

Running the example code in a web browser

  1. Run npm install
  2. Run npm install -g webpack
  3. Run the webpack command in the project root directory.
  4. Open the test/index.html file using a web browser (Only tested using Brave browser)
  5. Paste your JSON into the first two text areas and click Execute

FAQs

Package last updated on 30 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc