2

We have an app that generates complex JSON objects whose properties and values have been converted to hexadecimals. How can I revert them them to strings?

Sample Object:

{
    "636f756e747279": 
    {
        "6e616d65": "43616e616461",
        "636f6465": "4341"
    },
    "617574686f72":
    {
        "6e616d65": "4a61736d696e652048656174686572",
        "67656e646572": "66",
        "626f6f6b496473":
        [
            "6a65683233696f",
            "33393233393130"
        ]
    }
}

Code so far:

var data = require( './data.json' );
var parsed = {};

Object.keys( data ).forEach( function( key, index, keys )
{
    var prop = new Buffer( key, 'hex' ).toString();
    var value = new Buffer( data[ key ], 'hex' ).toString();

    parsed[ prop ] = value;
});

console.log( parsed );

But this code only works on simple JSON objects with simple key-value pairs.

3 Answers 3

1

You need to do type check and recursion. Although this code did not use it, I also suggest you to use underscore.js to simplify looping and type checking.

var data = {
  "636f756e747279":
  {
    "6e616d65": "43616e616461",
    "636f6465": "4341"
  },
  "617574686f72":
  {
    "6e616d65": "4a61736d696e652048656174686572",
    "67656e646572": "66",
    "626f6f6b496473":
    [
        "6a65683233696f",
        "33393233393130"
    ]
  }
};

var unpack = function(hex) {
  return new Buffer( hex, 'hex' ).toString();
};

var convert_object = function(data) {
  if (typeof data === 'string') {
    return unpack(data);
  } else if (Array.isArray( data )) {
    return data.map(convert_object);
  } else if (typeof data === 'object') {
    var parsed = {};

    Object.keys( data ).forEach( function( key, index, keys ) {
       parsed[ unpack(key) ] = convert_object( data[key]);
    });

    return parsed;
  } else {
    throw ("Oops! we don't support type: " + (typeof data));
  }
};

console.log( convert_object(data) );
Sign up to request clarification or add additional context in comments.

2 Comments

Works perfect, though instead of throwing error on unsupported type, we can just return the data. I encountered errors on type Number in some of my samples, for instance.
yep, I was wondering how would you encode number in hex string.
1

Your parser function is valid, but it needs to handle objects and arrays, and be called recursively:

function parse(obj) {

    var parsedObj = {};

    // If it's a string, just de-hexify it
    if (typeof(obj) == 'string') {
        return new Buffer( obj, 'hex' ).toString();
    }

    // Otherwise, parse each key / value
    Object.keys( obj ).forEach( function( key )
    {

        // Translate the key
        var prop = new Buffer( key, 'hex' ).toString();

        // Get the value
        var value = obj[key];

        // If value is an array, recursively parse each value
        if (Array.isArray(value)) {
            parsedObj[prop] = value.map(parse);
        }
        // Otherwise recursively parse the value, which is an object or array
        else {
            parsedObj[prop] = parse(value);
        }
    });

    // Return the result
    return parsedObj;
}

1 Comment

Everything good except that Array recursion needs to be outside the Object iterator. Otherwise, when an array is passed to the function, it will try to decode the array keys as hex (which are integers).
0

You should use the reviver parameter to the JSON.parse function:

var file = require('fs').readFileSync('./data.json', 'utf8');

function unpack(hex) {
    return new Buffer( hex, 'hex' ).toString();
}

var data = JSON.parse(file, function(k, v) {
    if (k == "" || Array.isArray(this)) return v;
    if (typeof v == "string")
        v = unpack(v);
    this[unpack(k)] = v;
});

console.log(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.