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 🙏

© 2024 – Pkg Stats / Ryan Hefner

obj2skv

v0.1.2

Published

Convert an object to simple key value pairs and back

Downloads

5

Readme

obj2skv

Convert an object to simple key value pairs and back. This should be helpful if you want to save an object as multiple rows in relational db, like Mysql.

What are "obj" and "skv" mean?

obj just means object, in json, it's like:

{ 	
	a: 1, 
	b: {
		c: [1, 2]
	}
}

skv means simple key value pairs, or simple hash object, like this:

{
	k1: v1,
	k2: v2,
	k3: v3
}

The key point of skv is "simple" key/value type, keys are just strings, values are just simple type values, e.g. string, number, no object or array allowed.

obj2skv provides two-way conversion functions, which convert object to skv and back.

###Why useful?

Given an object like this:

{
	a: 1,
	b: [ 'x', 1, 2.4, 100, true ],
	c: { 
		d: { f: 'hello' },
		z: 'hi' 
	} 
}

How to save it in Mysql? As we know, Mysql is a relational database, it saves data in 2-dimensional tables, but an arbitrary object is not 2-dimensional restricted, it's n-dimensional. By converting an object to skv, it's can be saved in 2 columns, one for key, and one for value.

###How it works?

To get a value in an object obj, there will be a path of keys, like this:

obj[k1][k2][k3] = v 

In skv, the keys are joined into a single key, like this:

skv[k1.k2.k3] = v

With an example, Object:

{ 	
	a: 1, 
	b: {
		c: "str"
	}
}

Convert to skv, which will be like:

{ 
	'a': 1, 
	'b.c': "str"
}

#####What about column type?

If to save the skv as 2 columns in mysql, one for key, one for value, the column type should be the same. Keys are all strings, no problem, but the values can be string, number...To keep the type information, and make the values got the same type, some prefixes are added, and values all become strings, for example,

  • Int 1 becomes "I1", with prefix "I" to identify int type
  • String "hello" becomes "Shello", with prefix "S" to identify string type

###A real example

Object:

{
    "a":null,
    "b":[
        "x",
        1,
        2.4,
        1e2,
        true
    ],
    "c":{
        "d":{
            "f":"hello"
        },
        "z":"hi"
    }
}

Convert to skv:

{ 
	'$a': 'N', //N just means null type
	'$b.@0': 'Sx', //key prefix '@' means it's an element of array
	'$b.@1': 'I1', //I means int type
	'$b.@2': 'F2.4', //F means float type
	'$b.@3': 'I100',
	'$b.@4': 'B1', //B means boolean type
	'$c.$d.$f': 'Shello', //S means string type
	'$c.$z': 'Shi'
}

How to use?

obj2skv got JS(works on Browser/Node) and PHP version

####Browser

<script src="/js/obj_skv_helper.js"></script>

<script>
//convert an object to skv
var simplekv = objSkvHelper.obj2simplekv(obj);

//convert skv back to object
var newobj = objSkvHelper.simplekv2obj(simplekv);
</script>

####Node

var objSkvHelper = require('/js/obj_skv_helper.js');

//convert an object to skv
var simplekv = objSkvHelper.obj2simplekv(obj);

//convert skv back to object
var newobj = objSkvHelper.simplekv2obj(simplekv);

####PHP

require '/php/obj_skv_helper.php';

//convert an object to skv
$simplekv = ObjSkvHelper::obj2simplekv ( $obj );

//convert skv back to object
$newobj = ObjSkvHelper::simplekv2obj ( $simplekv );