7

I have a JSON data structure as shown below:

{
    "name": "World",
    "children": [
      { "name": "US",
          "children": [
           { "name": "CA" },
           { "name": "NJ" }
         ]
      },
      { "name": "INDIA",
          "children": [
          { "name": "OR" },
          { "name": "TN" },
          { "name": "AP" }
         ]
      }
 ]
};

I need to change the key names from "name" & "children" to say "key" & "value". Any suggestion on how to do that for each key name in this nested structure?

2
  • When/where do you want to make the change? Commented Nov 22, 2012 at 19:19
  • ...and why do you have a semicolon at the end? What exactly are you representing in the question? Commented Nov 22, 2012 at 19:33

3 Answers 3

17

I don't know why you have a semicolon at the end of your JSON markup (assuming that's what you've represented in the question), but if that's removed, then you can use a reviver function to make modifications while parsing the data.

var parsed = JSON.parse(myJSONData, function(k, v) {
    if (k === "name") 
        this.key = v;
    else if (k === "children")
        this.value = v;
    else
        return v;
});

DEMO: http://jsfiddle.net/BeSad/

Sign up to request clarification or add additional context in comments.

5 Comments

thank you. your code is working perfect. Sorry for semicolon at the end. It was a typo, but I've learned so much from this mistake.
@IHateLazy: I see something new like changing the key but may I know where is this function so that I can more in details. And if I want to add number to the key in the for loop, I seem can't get it to work like -- this.batchi.concat(string(i)) = v; where i is incrementing in the for loop.
@IHateLazy: Please refer to link.
Does anyone know why dots cannot be replaced in the key name, but any other letter can? jsfiddle.net/dpiret/dgk5fp16/5
@danielp why did you use k.search? That function works with regex (so providing it with '.' gave you unexpected results. You probably wanted to use k.indexOf('.')
0

Try this:

function convert(data){
  return {
    key: data.name,
    value: data.children.map(convert);
  };
}

Or if you need to support older browsers without map:

function convert(data){
  var children = [];
  for (var i = 0, len = data.children.length; i < len; i++){
    children.push(convert(data.children[i]));
  }

  return {
    key: data.name,
    value: children
  };
}

Comments

0

You could use a function like this :

function clonerename(source) {
    if (Object.prototype.toString.call(source) === '[object Array]') {
        var clone = [];
        for (var i=0; i<source.length; i++) {
            clone[i] = goclone(source[i]);
        }
        return clone;
    } else if (typeof(source)=="object") {
        var clone = {};
        for (var prop in source) {
            if (source.hasOwnProperty(prop)) {
                var newPropName = prop;
                if (prop=='name') newPropName='key';
                else if (prop=='children') newPropName='value';
                clone[newPropName] = clonerename(source[prop]);
            }
        }
        return clone;
    } else {
        return source;
    }
}

var B = clonerename(A);

Note that what you have isn't a JSON data structure (this doesn't exist as JSON is a data-exchange format) but probably an object you got from a JSON string.

4 Comments

The only thing I see that would disqualify it as valid JSON markup is the semicolon (I have no idea why that's there). Other than that, why wouldn't this be valid JSON?
Because JSON is a string format. This is probably a simple javascript object, it's only missing some var a = before. hence the ;.
dystroy: Yes, JSON is a text format, but that doesn't mean this isn't some JSON markup sitting in a file on the server, again except for that semicolon. It's really not valid either way as shown.
...but I guess I'd agree that the ; could make me lean that way.

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.