data structures - Serializing rows of key/value pairs into a JSON object -


i need transforming table of data:

[     {property:"key", content:"1"},     {property:"key", content:"2"},     {property:"key2", content:"3"},     {property:"key2:subkey", content:"4"},     {property:"key2:someother:key", content:"5"},     {property:"foo", content:"6"},     {property:"foo", content:"7"},     {property:"foo:bar", content:"8"} ] 

into json object following structure:

{   key: ["1", "2"],   key2: {     '': "3"     subkey: "4"     someother: {       key: "5"     }   },   foo: [     "6",     {       '': "7"       bar: "8"     }   ] } 

here rules. note: rules apply level in json object (json.levelone, json.level.two, json.level.three.even, etc)

  1. for each row.property "a:b:c" should translate json.a.b.c = row.content.
  2. when row.property = "x" , json.x !== undefined json.x = [json.x, row.content]
  3. whenever json.x === "string" , row.property = "x:y" json.x = {'': json.x, y: row.content}
  4. whenever array.isarray(json.x) && json.x[json.x.length-1] === "string" , row.property = "x:y" json.x[json.x.length-1] = {'': json.x[json.x.length-1], y: row.content}

hopefully gives idea criteria of need translate data json object format.

why?

i'm trying take open graph meta data , serialize json object. feel format above best reflect open graph meta data structure. need writing algorithm though. open source node.js project i'm working on.

all appreciated. thanks!

edit

so there issue left parser. arrays occur @ leaf nodes in cases.

here project on github: https://github.com/samholmes/node-open-graph feel free fork it, build better parse, , send me pull request.

updated per our discussion on irc

var data = [     {property:"key", content:"1"},     {property:"key", content:"2"},     {property:"key2", content:"3"},     {property:"key2:subkey", content:"4"},     {property:"key2:someother:key", content:"5"},     {property:"foo", content:"6"},     {property:"foo", content:"7"},     {property:"foo:bar", content:"8"},     {property:"foo:baz", content:"9"} ];  var transformed = {};  data.foreach(function (item) {     var key, tmp,         ptr = transformed,         keys = item.property.split(':');      // want leave 1 key assign use references     // long there's 1 key left, we're dealing sub-node , not value      while (keys.length > 1) {         key = keys.shift();          if (array.isarray(ptr[key])) {             // last index of ptr[key] should become             // object examining.             tmp = ptr[key].length-1;             ptr = ptr[key];             key = tmp;         }          if (typeof ptr[key] === 'string') {             // if it's string, convert             ptr[key] = { '': ptr[key] };         } else if (ptr[key] === undefined) {             // create new key             ptr[key] = {};         }          // move our pointer next subnode         ptr = ptr[key];     }      // deal last key     key = keys.shift();      if (ptr[key] === undefined) {         ptr[key] = item.content;     } else if (array.isarray(ptr[key])) {         ptr[key].push(item.content);     } else {         ptr[key] = [ ptr[key], item.content ];     } }); console.log(transformed); 

outputs:

{     key: ['1', '2'],     key2: {         '': '3',         subkey: '4',         someother: {             key: '5'         }     },     foo: ['6', {         '': '7',         bar: '8'         baz: '9'     }] } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -