0

How can I change the name of a node in my JSON?

My code:

childType = view
childName = view0

child=[];
    child[childTipy]= { 
                childType:{
                    "tipo": childTipy,
                    "nome":childName,
                }
            };

childList.push(child[childTipy]);

minhasWindows =  {"window": {
                        "nome": "Win2",
                        "title": "Win",
                        "childrens": childList
                    }
};

The resulting JSON:

{
    "windows" : [
        {
            "window" : {
                "nome" : "Win2",
                "title" : "Win",
                "childrens" : [
                    {
                        "childType" : {
                            "tipo" : "view",
                            "nome" : "view0"
                        }
                    }
                ]
            }
        }
    ]
} 

I want the childType node to be the value of my var childType = "view". How can I change this?

PS: I have multiple childType values.

Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
user2403131
  • 43
  • 1
  • 9
  • I would highly recommend not creating and editing JSON yourself. JQuery has a bunch of built in functions to serialize and deserialize JSON for you. – thatidiotguy May 20 '13 at 20:24
  • 1
    There is no jQuery here. – Lee Taylor May 20 '13 at 20:24
  • 1
    @LeeTaylor Its one of the tags of the question. – thatidiotguy May 20 '13 at 20:25
  • The question is confusing. Are you trying to use a variable as a key in a object? If so, see http://stackoverflow.com/questions/2462800/how-to-create-a-dynamic-key-to-be-added-to-a-javascript-object-variable – bfavaretto May 20 '13 at 20:33
  • What is the relationship between the first and second code snippets? Are you creating JSON from JavaScript objects? Or is it the other way around? –  May 20 '13 at 20:52

4 Answers4

0

Change this

childType:{

to

view:{
cyber_rookie
  • 665
  • 5
  • 9
0

Replace

child=[];
child[childTipy]= { 
            childType:{
                "tipo": childTipy,
                "nome":childName,
            }
        };

with

child=[];
child[childTipy]= {};
child[childTipy][childType] = {
                "tipo": childTipy,
                "nome":childName,
            };
Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
0

If you want the property to come from a variable, use array syntax to assign the property.

var childType = "view"; // String needs to be quoted
var childName = "view0"; // String needs to be quoted
var child = {}; // This must be an object, not array
child[childType] = {
   tipo: childType, // Don't need to quote keys in object literals
   nome: childName,
};
childList.push(child);

You also had too many levels in your child object.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • My json return: {"windows":[{"window":{"nome":"Win2","title":"Win","width":"auto","height":"auto","fullscreen":"true","modal":"true","main":"true","childrens":[[]]}}]} i'm use add this "childList.push(child[childType]);" – user2403131 May 20 '13 at 20:45
  • Is `childTypy` the same as `childType`? – Barmar May 20 '13 at 20:48
  • Is `childTypy` a number or string? You can't use a string as an index in an array, so you can't initialize `var child = [];`. If it's a string `child` has to be an object, `var child = {};`. – Barmar May 20 '13 at 20:54
  • I didn't ask about `childType`, I asked about `childTypy`. Or is one of those a typo? – Barmar May 20 '13 at 20:57
  • I have "N" elements with different values ​​for both properties as to the name of "childType", what need is the node "childType" is the value of the variable "childType" – user2403131 May 20 '13 at 21:07
  • This general structure should work. If it doesn't, please update your question with your attempt to use it. – Barmar May 20 '13 at 21:08
-1

If childType appears in the keys only:

var someJson = '{"windows":[{"window":{"nome":"Win2","title":"Win","childrens":[{"childType":{"tipo":"view","nome":"view0"}}]}}]}';

var newJson = $.parseJSON(someJson.replace('childType', 'view'));

Though I don't see any actual need of changing a key in place.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • Uncaught TypeError: Object # has no method 'replace' – user2403131 May 20 '13 at 20:39
  • Be careful: in my code `someJson` is a JSON string, not an object. If you have an object already, serialize it to a string before [e.g. `JSON.stringify(someJson)`]. – moonwave99 May 20 '13 at 20:42
  • 1
    Doing a string replacement in the JSON string seems like a very dangerous way to implement this. – Barmar May 20 '13 at 20:52
  • @Barmar actually _this_ should not be implemented at all, because there is a clear design flaw at the top - this is just a dirty hack. – moonwave99 May 20 '13 at 20:53