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

object-unpacker

Package Overview
Dependencies
Maintainers
2
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

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-40%
Maintainers
2
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 TypeScript example follows:

// Import the Schema Mapper type
import { createObjectUnpacker } from '../src/ObjectUnpacker';
import { ProcedureInstruction } from '../src/instructions';

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

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

// This is the transformation specification
const mapperData: ProcedureInstruction = [
  {
    comment: 'Create the top level result object',
    action: 'toObject',
    source: '/compact',
    target: '/expanded',
    keys: ['x'],
    values: {
      formatted: '%x'
    }
  },
  {
    comment: 'process the `formatted` array',
    action: 'foreach',
    source: '/expanded.formatted',
    target: '/expanded.formatted',
    loopVar: 'i',
    instruction: [
      {
        comment: 'process the array entry',
        action: 'toObject',
        source: 'i',
        target: 'tmp',
        keys: ['a', 'au'],
        values: {
          another:{
            first:'%a.0',
            second:'%a.1',
            fourth:'%/subs.system.metadata.name'
          },
          author:'%au'
        }
      }
    ]
  }
];

// 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));

The same example in JavaScript

const unpacker = require("object-unpacker")

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

// This is the transformation specification
ProcedureInstruction = [
  {
    comment: 'Create the top level result object',
    action: 'toObject',
    source: '/compact',
    target: '/expanded',
    keys: ['x'],
    values: {
      formatted: '%x'
    }
  },
  {
    comment: 'process the `formatted` array',
    action: 'foreach',
    source: '/expanded.formatted',
    target: '/expanded.formatted',
    loopVar: 'i',
    instruction: [
      {
        comment: 'process the array entry',
        action: 'toObject',
        source: 'i',
        target: 'tmp',
        keys: ['a', 'au'],
        values: {
          another:{
            first:'%a.0',
            second:'%a.1',
            fourth:'%/subs.system.metadata.name'
          },
          author:'%au'
        }
      }
    ]
  }
];
// 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 = unpacker.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:

{{
  "formatted": [
    {
      "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 npm install -g webpack-cli
  4. Run the webpack command in the project root directory.
  5. Open the test/index.html file using a web browser (Only tested using Brave browser)
  6. Paste your JSON into the first two text areas and click Execute

FAQs

Package last updated on 09 Jun 2022

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