1

I'm not overly knowledgeable in JavaScript, but here's the rundown on what I'm trying to do.

I have a JSON object that is prepared by an external source. I'm using that Object as-is to string on to another JSON object that I have prepared, but the problem is that each of the keys need to have a namespace abbreviation in front of them.

So this is what I have:

obj1 = {
  "key1": "value",
  "key2": "value",
  "key3": "value",
  "key4": {
   "subkey1": "value",
   "subkey2": "value"
  }
}

And what I want it to turn into:

obj1 = {
  "ns:key1": "value",
  "ns:key2": "value",
  "ns:key3": "value",
  "ns:key4": {
   "ns:subkey1": "value",
   "ns:subkey2": "value"
  }
}

This should be done dynamically, since this will be used for several different aspects of the same project. So I'm hoping to simply have a function that I pass 'obj1' into and have it spit out the converted JSON Object, no matter what I give it.

What would the easiest way to go about this be? Again, I'm not used to JavaScript, so examples are welcome.

PS. A CoffeeScript solution is also acceptable. I'm basically writing it out in JS then converting this using js2coffee.org.

Drew Edgar
  • 87
  • 1
  • 6
  • Do you want to mutate the objects or create changed copies of them? – Bergi Jun 19 '13 at 19:54
  • Well `for ... in` will let you iterate over the properties, so that's really all there is to it. If you want to do the change "in place", you'd add a new property and then `delete` the old one. – Pointy Jun 19 '13 at 19:56
  • "*I'm using that Object as-is to string on to another JSON object that I have prepared*" - can you show us (by code) what you are doing exactly? – Bergi Jun 19 '13 at 19:56
  • There's really no such thing as a "JSON object" in the context of JavaScript code at runtime. You can serialize to or from JSON, but when you parse the JSON you've got a plain JavaScript object. – Pointy Jun 19 '13 at 20:01
  • @Bergi basically I'm just dropping obj1 into another object using obj1 as the value. biggerObj = { "ns:key": "value", "ns:key2": obj1, "ns:key3": "value" } – Drew Edgar Jun 19 '13 at 20:07
  • @pointy how would I go about doing that? And sorry, yes JavaScript Object. To me, they're the same thing because they look and act the same in every capacity that I've used them in my limited experiences – Drew Edgar Jun 19 '13 at 20:07
  • @DrewEdgar well the answer that Antonis posted is a good sample. – Pointy Jun 19 '13 at 20:11
  • @DrewEdgar: by "*dropping into*" you mean copying keys, or setting a (new) property? Please post your code, it might be easy to integrate. – Bergi Jun 19 '13 at 22:01

1 Answers1

4

Try to check the code to see if I missed any unhandled cases. You have to check for a few things before you use it I guess...

var obj1 = {
  "key1": "value",
  "key2": "value",
  "key3": "value",
  "key4": {
   "subkey1": "value",
   "subkey2": "value"
  }
};

var rename = function(obj, prefix){

    if(typeof obj !== 'object' || !obj){
        return false;    // check the obj argument somehow
    }

    var keys = Object.keys(obj),
        keysLen = keys.length,
        prefix = prefix || '';

    for(var i=0; i<keysLen ;i++){

        obj[prefix+keys[i]] = obj[keys[i]];
        if(typeof obj[keys[i]]=== 'object'){
            rename(obj[prefix+keys[i]],prefix);
        }
        delete obj[keys[i]];
    }

    return obj;
};
AntouanK
  • 4,880
  • 1
  • 21
  • 26