Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

one-hot-json

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

one-hot-json

flattening JSON Objects and encoding values via one-hot approach

latest
Source
npmnpm
Version
0.0.3
Version published
Maintainers
1
Created
Source

one-hot-json

on development

Purpose

flattening and encoding JSON Objects

Spark of Need

Many multivariate algorithms depend on array-like data. There is a need for modules to transform JSON Objects into such kind of data in a reasonable and easy to use manner. The main goal is to deliver a module with a generic encoding and decoding functionality for a collection of JSON Objects of the same Schema.

Approach

Flattening JSON Objects and encoding string values via one-hot approach.

Example

const Onehot = require('one-hot-json');

const input = [
  {
    user: { id: 1, name: 'Dalton', prename: 'John', online: true },
    project: { id: 1, name: 'lodash'},
  },
  {
    user: { id: 1, name: 'Dalton', prename: 'John', online: true },
    project: { id: 2, name: 'docdown'},
  },
  {
    user: { id: 1, name: 'Dalton', prename: 'John', online: true },
    project: { id: 3, name: 'lodash-cli'},
  },
  {
    user: { id: 2, name: '-', prename: 'contra', online: false },
    project: { id: 4, name: 'gulp'},
  },
  {
    user: { id: 3, name: '-', prename: 'phated', online: true },
    project: { id: 4, name: 'gulp'},
  }
];

Initiate the one-hot-json transcriptor by a sample from targeting collection.

let sample = _.first(input);
const User = Onehot(sample);

let encoded:  = User.encode(input);
// encoded:
[ [ 1, 0, 0, 2, 1, 0 ],
  [ 1, 0, 0, 2, 2, 1 ],
  [ 1, 0, 0, 2, 3, 2 ],
  [ 2, 1, 1, 3, 4, 3 ],
  [ 3, 1, 2, 2, 4, 3 ] ]

Decoding:

let decoded = User.decode(encoded);
console.log(JSON.stringify(input) === JSON.stringify(decoded));
//true

Usecase: encoded matrix as output from a replication or machine learning algorithms.

let deformatedEncoding = [
  [ 0.9, 0.1, 0.2, 2.3, 0.8, 0.01 ],
  [ 1.1, 0.2, 0.3, 2.2, 2.1, 1.35 ],
  [ 0.8, 0.1, 0.1, 2.3, 3.2, 2.11 ],
  [ 1.9, 0.9, 1.2, 3.1, 4.2, 3.31 ],
  [ 2.9, 1.1, 2.2, 2.1, 4.3, 3.22 ]
];

let decodedDeformation = User.decode(deformatedEncoding);
console.log(JSON.stringify(input) === JSON.stringify(decodedDeformation));
//true

Keywords

json

FAQs

Package last updated on 07 Nov 2016

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