-2

I have this Js array:

const a = [
  [
    "Paris",
    "75000"
  ],
  [
    "Toulouse",
    "31000"
  ],
  [
    "Marseille",
    "13000"
  ]
];

How to convert restructure this array to JSON?

[{
  "city": "Paris",
  "zip": "75000"
},
{
  "city": "Toulouse",
  "zip": "31000"
},
{
  "city": "Marseille",
  "zip": "13000"
}]

I tried with the JSON.stringify() function but I don't get the expected result.

Thanks

traktor
  • 17,588
  • 4
  • 32
  • 53
medah
  • 27
  • 4
  • The array that you already have 1) is not a js array and 2) overwrites `tab[0]` on each line, so at best your result is just `[{city: 'Marseille', zip: '13000'}]` (which it isn't, because #1). Edit: well, I guess _maybe_ it is an array, an array of functions with string constants as the parameter? But I doubt that's what you expect it to be. – Tom Mar 27 '22 at 20:53
  • Get familiar with [how to access and process objects, arrays, or JSON](/q/11922383/4642212) and how to create [objects](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer) or [arrays](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/Array#array_literal_notation) and use the static and instance methods of [`Object`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Mar 27 '22 at 20:54
  • This does not look entirely like JavaScript code... – Arnav Thorat Mar 27 '22 at 20:56
  • @Tom: Sorry I got confused, I also want to give custom indexes (city, zip) in the final result. I have edited – medah Mar 27 '22 at 21:34
  • Assuming you have an array in JavaScript, named `a` for example, please edit the question and replace the array content (starting with `[0][ "Paris",`) with the result of executing `JSON.stringify(a,null,2)` in the JavaScript environment where `a` exists. – traktor Mar 27 '22 at 23:33
  • @traktor ok, I modified the js code – medah Apr 03 '22 at 16:56

2 Answers2

0

Youre array declaration isn't correct. This is the correct syntax for declaring an array in JS and using JSON.stringify on it:

tab = [
  {city: 'Paris', zip: '75000'},
  {city: 'Toulouse', zip: '31000'},
  {city: 'Marseille', zip: '13000'}
];

JSON.stringify(tab, null, 2)
SteveGreenley
  • 630
  • 1
  • 5
  • 7
-1

You could use Array.prototype.map to convert sub-array entries of the original array into objects with suitably named properties, and call JSON.stringify on the result.

const tabs = [
  [
    "Paris",
    "75000"
  ],
  [
    "Toulouse",
    "31000"
  ],
  [
    "Marseille",
    "13000"
  ]
];

// restructure sub-arrays into objects:

let objectArray = tabs.map(entry=> {
    const [city, zip] = entry;
    return {city, zip};
})

// Stringify object array

let jsonText = JSON.stringify( objectArray, null, 2)
console.log(jsonText);  

The map function is using Destructuring assignment to extract city and zip values from each sub-array.

A null second and numeric third parameter supplied to JSON.stringify improve human readability of the output but are generally omitted in production environments to reduce the length of network messages.

traktor
  • 17,588
  • 4
  • 32
  • 53