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 🙏

© 2025 – Pkg Stats / Ryan Hefner

object-unpacker

v1.0.2

Published

A JSON to JSON mapper in TypeScript

Downloads

15

Readme

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