Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

364
Visualizações
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?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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)
about 4 years ago · Juan Pablo Isaza Relatório

0

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).

about 4 years ago · Juan Pablo Isaza Relatório

0

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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda