2

I am looking to rename a variable with a different name depending on its level within a nested JSON dataset.

An example JSON file is the following:

[
  {
    key: "John Doe School",
    values: [
      {
        key: "Mr. Brown",
        values: [
          {
            key: "Joe",
            TestScore: 95
          },
          {
            key: "Sarah",
            TestScore: 99
          }
        ]
      }
    ]
  }
]

In this example, I would like to change the first-level "key" to "School", the second-level "key" to "Teacher", and the third-level "key" to "Student".

The JSON dataset would look like this following the changes:

[
  {
    School: "John Doe School",
    values: [
      {
        Teacher: "Mr. Brown",
        values: [
          {
            Student: "Joe",
            TestScore: 95
          },
          {
            Student: "Sarah",
            TestScore: 99
          }
        ]
      }
    ]
  }
]
nyan22
  • 25
  • 1
  • 5
  • What have you tried? – nicael Jan 17 '16 at 19:19
  • I attempted to the implement the solutions from the following two posts: 1.) http://stackoverflow.com/questions/21148419/efficiently-rename-re-map-javascript-json-object-keys-within-structured-array, 2.) http://stackoverflow.com/questions/4647817/javascript-object-rename-key. However, they do not seem to address the nesting problem, since "key" would be renamed in the same way at every level. – nyan22 Jan 17 '16 at 19:29
  • So? What's your problem? – nicael Jan 17 '16 at 19:31
  • The above links do not address the nesting problem, since "key" would be renamed in the same way at every level. – nyan22 Jan 17 '16 at 19:33

1 Answers1

3

Well, given your example, you could just do this...

var json = [
  {
    key: "John Doe School",
    values: [
      {
        key: "Mr. Brown",
        values: [
          {
            key: "Joe",
            TestScore: 95
          },
          {
            key: "Sarah",
            TestScore: 99
          }
        ]
      }
    ]
  }
];

json[0].key = "School";
json[0].values[0].key = "Teacher";
json[0].values[0].values[0].key = "Student";
json[0].values[0].values[1].key = "Student";

UPDATE FOLLOWING COMMENT

json[0]['School'] = json[0].key; // Add new key:value
delete json[0].key; // Delete previous one

json[0].values[0]['Teacher'] = json[0].values[0].key; // Add new key:value
delete json[0].values[0].key; // Delete previous one

json[0].values[0].values[0]['Student'] = json[0].values[0].values[0].key; // Add new key:value
delete json[0].values[0].values[0].key; // Delete previous one

json[0].values[0].values[1]['Student'] = json[0].values[0].values[1].key; // Add new key:value
delete json[0].values[0].values[1].key; // Delete previous one

I assume you will be looping through a number of different JSON objects however, so you would of course also need to implement that but the above structure should point you in the right direction ;)

ann0nC0d3r
  • 316
  • 1
  • 11
  • Thanks very much for the suggestion. The problem is that this solution is overwriting the values associated with "key" rather than the variable name for "key". See here: https://jsfiddle.net/nyan22/u0qnj7vb/6/. I have re-posted the question to make it the desired output more clear. – nyan22 Jan 17 '16 at 20:07
  • Thanks very much. This solution helped a lot! – nyan22 Jan 17 '16 at 20:35
  • :) Glad it helped... happy coding! – ann0nC0d3r Jan 17 '16 at 20:36