简体   繁体   中英

Remove JSON parent element in JavaScript

I'm currently trying to code an application with javascript. It pulls data from a database and the response I'm getting is something like that:

    {
       "values":[
           {
               "name": "Munich",
               "location": "Germany",
               "native_lang": "German",
           },
           {
               "name": "London",
               "location": "England",
               "native_lang": "English",
           },
           {
               "name": "Rome",
               "location": "Italy",
               "native_lang": "Italian",
           }
       ]
    }

But I need to have the JSON like that:

    [
       {
           "name": "Munich",
           "location": "Germany",
           "native_lang": "German",
       },
       {
           "name": "London",
           "location": "England",
           "native_lang": "English",
       },
       {
           "name": "Rome",
           "location": "Italy",
           "native_lang": "Italian",
       }
    ]

How can I delete the parent values object in my JSON?

SHORT ANSWER:

Just access the values property like a JavaScript object.

LONG ANSWER:

You didn't post the JavaScript code snippet so it's quite difficult to give you an appropriate answer.

Assuming you have the following code:

const jsonString = getDataFromTheDB()
const jsonObject = JSON.parse(jsonObject) // still has the "values" layer
const values = jsonObject.values // what you want, without the "values" layer

// BONUS: Just in case you want to convert the object back to a JSON string but without the "values" layer
const valuesJSON = JSON.stringify(values, undefined, 2)

Based on this post :

just do this (consider json the variable that contains your json):

var key = "values";
var results = json[key];
delete json[key];
json = results;

console.log(json) will output the following:

 [
           {
               "name": "Munich",
               "location": "Germany",
               "native_lang": "German",
           },
           {
               "name": "London",
               "location": "England",
               "native_lang": "English",
           },
           {
               "name": "Rome",
               "location": "Italy",
               "native_lang": "Italian",
           }
]

But you dont even have to do the last 2 steps of the code snippet above, you could also just directly use results variable and have the same output by console.log(results) .

You could take the object and create a new variable with just the array.

 var vals = { "values":[ { "name": "Munich", "location": "Germany", "native_lang": "German", }, { "name": "London", "location": "England", "native_lang": "English", }, { "name": "Rome", "location": "Italy", "native_lang": "Italian", } ] } var arr = vals.values; console.log(arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM