0

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?

axel
  • 3,778
  • 4
  • 45
  • 72
Nico
  • 323
  • 4
  • 14
  • 1
    That's not valid. If it's an array, it needs `[]` around it. – Barmar Oct 12 '21 at 16:03
  • @Barmar This is JavaScript. It does not distinguish between object property access and array index access. You would only need `[]` when either * The property name is not a valid identifier * Or, the property name is a value of an expression – DMaster Oct 12 '21 at 16:13
  • @DMaster I don't know what you're talking about. My comment was made before someone edited the question to put the objects in an array. – Barmar Oct 12 '21 at 16:15

3 Answers3

1

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)
DMaster
  • 603
  • 1
  • 10
  • 23
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).

L.Gashi
  • 183
  • 1
  • 11
-1

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);
Mr. Cooper
  • 364
  • 3
  • 12