# object-unpacker

> A JSON to JSON mapper in TypeScript

Latest version **1.0.2** (published 2022-06-09) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install object-unpacker
pnpm add object-unpacker
yarn add object-unpacker
bun add object-unpacker
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-06-09 |
| First published | 2021-03-30 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 75.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | NUM Technology Ltd |
| Maintainers | frednum, twalmsley |

## Links

- npm: https://www.npmjs.com/package/object-unpacker
- Repository: https://github.com/NUMtechnology/typescript-object-unpacker
- Homepage: https://github.com/NUMtechnology/typescript-object-unpacker#readme
- Issues: https://github.com/NUMtechnology/typescript-object-unpacker/issues
- npm.io page: https://npm.io/package/object-unpacker

## Dependencies (1)

- [num-easy-log](https://npm.io/package/num-easy-log.md) ^0.0.2

## Recent versions

- 1.0.2 (latest) — 2022-06-09
- 1.0.1 — 2022-04-26
- 1.0.0 — 2022-04-19
- 1.0.0-rc.3 — 2022-04-08
- 1.0.0-rc.2 — 2022-04-07
- 1.0.0-rc.1 — 2022-04-07
- 0.0.22 — 2022-02-09
- 0.0.21 — 2022-02-09
- 0.0.18 — 2021-09-29
- 0.0.17 — 2021-06-10
- 0.0.16 — 2021-06-10
- 0.0.15 — 2021-05-27
- 0.0.14 — 2021-05-20
- 0.0.13 — 2021-05-17
- 0.0.11 — 2021-05-12
- … 10 more at https://npm.io/package/object-unpacker/versions

## 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:

```TypeScript
// 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`
```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:
```json
{{
  "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`

---
_Source: https://npm.io/package/object-unpacker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
