-1

I have a JSON that looks like this:

 [{
     "1": [{
         "lat": " -1.854029",
         "lng": " 36.488604"
     }, {
         "lat": " -1.519856",
         "lng": " 36.102752"
     }, {
         "lat": " -1.283394",
         "lng": " 36.657745"
     }]
 }, {
     "2": [{
         "lat": " -1.325416",
         "lng": " 36.669051"
     }, {
         "lat": " -1.392932",
         "lng": " 36.768752"
     }, {
         "lat": " -1.390505",
         "lng": " 36.810023"
     }, {
         "lat": " -1.448266",
         "lng": " 36.952769"
     }, {
         "lat": " -1.267033",
         "lng": " 37.094882"
     }, {
         "lat": " -1.214605",
         "lng": " 37.053978"
     }, {
         "lat": " -1.169516",
         "lng": " 36.895608"
     }]
 }]

 I want to convert it to this format.

 {
     "1": [{
         "lat": " -1.854029",
         "lng": " 36.488604"
     }, {
         "lat": " -1.519856",
         "lng": " 36.102752"
     }, {
         "lat": " -1.283394",
         "lng": " 36.657745"
     }],
     "2": [{
         "lat": " -1.325416",
         "lng": " 36.669051"
     }, {
         "lat": " -1.392932",
         "lng": " 36.768752"
     }, {
         "lat": " -1.390505",
         "lng": " 36.810023"
     }, {
         "lat": " -1.448266",
         "lng": " 36.952769"
     }, {
         "lat": " -1.267033",
         "lng": " 37.094882"
     }, {
         "lat": " -1.214605",
         "lng": " 37.053978"
     }, {
         "lat": " -1.169516",
         "lng": " 36.895608"
     }]
 }

I have tried JSON.parse(var) but it does not give the required output. If I try to access a value in my json, it gives undefined. Please someone help no what am doing wrong. Is it the wat am creating the json? If so how should I create it so I get the required output as mentioned above. Or is the problem with the way am parsing the json?

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
Elizabeth
  • 31
  • 1
  • 10

2 Answers2

1

You're actually working with an array that contains javascript object, use myArray[0] to get the object:

var myArray =  [
        {
            "1": [
                {
                    "lat": " -1.854029",
                    "lng": " 36.488604"
                },
                {
                    "lat": " -1.519856",
                    "lng": " 36.102752"
                },
                {
                    "lat": " -1.283394",
                    "lng": " 36.657745"
                }
            ]
        },
        {
            "2": [
                {
                    "lat": " -1.325416",
                    "lng": " 36.669051"
                },
                {
                    "lat": " -1.392932",
                    "lng": " 36.768752"
                },
                {
                    "lat": " -1.390505",
                    "lng": " 36.810023"
                },
                {
                    "lat": " -1.448266",
                    "lng": " 36.952769"
                },
                {
                    "lat": " -1.267033",
                    "lng": " 37.094882"
                },
                {
                    "lat": " -1.214605",
                    "lng": " 37.053978"
                },
                {
                    "lat": " -1.169516",
                    "lng": " 36.895608"
                }
            ]
        }
    ];

myArray[0] will return the object that you can use without the need to parse. Parsing is used when we have the JSON (i.e string notation) and we have to get the corresponding js object or vice versa.

KAD
  • 10,972
  • 4
  • 31
  • 73
0

I think this solution could help your problem: JSON.parse(JSON.stringify(array))

Please check if this could solve your issue: Convert array to JSON

Community
  • 1
  • 1
Arindam
  • 555
  • 1
  • 8
  • 24