Split a pipe delimited key-value pair separated by '=' symbol

We are receiving an input parameter value as a pipe-delimited key-value pair, separated with = symbols. For example:

"|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|"

So the format is: |KEY=VALUE|KEY_2=VALUE_2|....|KEY_n=VALUE_n|

I need to split it into a JSON object. So, my object should be :

{
 'User':'0101',
 'Name':'ImNewUser',
 'IsAdmin':'0',
 'RefId'='23ae2123cd223bf235'
}

What will be best way to go, since there are multiple options:

  • I can use split with | and again on each element split with =.
  • I can depend on regular expression and do string replace.
  • Split it with = remove trailing | symbol and associate two different arrays with indexes.

Can anyone tell me the best/most efficient way of doing this in JavaScript (programming in Node.js)?

The first one sounds good:

var str = "|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|";


var result = {};
str.split('|').forEach(function(x){
    var arr = x.split('=');
    arr[1] && (result[arr[0]] = arr[1]);
});

If you do decide to use regex, make sure it's block rockin' regex like this:

var result = {};

s.replace(/([^=|]+)=([^|]*)/g, function(noStep3, a, b) { result[a] = b; });

I would just use regular expressions to group (see here) each KEY=VALUE pair and then iterate over them to fill up the JSON object. So you could have something like this:

var re = /(([^=\|]+)=([^=\|]+))/g;
var match;
var myString = "|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|";
while (match = re.exec(myString)) {
    console.log(match);
    // first iteration returns ["User=0101","User=0101","User","0101"] and so on
}

var str = "|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|";


var result = {}, name;
str.substring(1, str.length-1).split(/\||=/).forEach(function(item, idx){
   idx%2 ? (result[name] = item) : (name = item); 
});

Cleanest way possible, you can modify the source to split by a different delimiter.

https://gist.github.com/allensarkisyan/5873977#file-parsequerystring-js

`/**
* @name - parseQueryString 
* @author - Allen Sarkisyan
* @license - Open Source MIT License
*
* @description - Parses a query string into an Object.
* - Optionally can also parse location.search by invoking without an argument
*/`

`
function parseQueryString(queryString) {
    var obj = {};
    function sliceUp(x) { x.replace('?', '').split('&').forEach(splitUp); }
    function splitUp(x) { var str = x.split('='); obj[str[0]] = decodeURIComponent(str[1]); }
    try { (!queryString ? sliceUp(location.search) : sliceUp(queryString)); } catch(e) {}
   return obj;
}
`

Replace the '|' with "','"; and = with ':';

Then replace the first ',' with x={', the last ',' with '} and evaluate the string.

Warning: There's of course a security issue involved, if somebody else can provide the data.

a=str.replace(/\|/g,"','").replace(/=/g,"':'");
eval('result={' + a.substring(2,a.length-2) + '}');

Don't know how to easily do this as a one-liner.