Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
String to non-array json

I have a string that looks like the following:

 {
  "results": [
    {
      "address_components": [
        {
          "long_name": "Ireland",
          "short_name": "Ireland",
          "types": [
            "establishment",
            "natural_feature"
          ]
        }
      ],
      "formatted_address": "Ireland",
      "geometry": {
        "bounds": {
          "northeast": {
            "lat": 55.38294149999999,
            "lng": -5.431909999999999
          },
          "southwest": {
            "lat": 51.4475448,
            "lng": -10.4800237
 

         }
            }],
    "status" : "OK"
   }

I want to turn this into a json that I can easily traverse. However when I call JSON.parse() on this string I get a json array that is a pain to traverse. I want to be able to extract the lat and lng from northeast without having to loop through the entire array.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

 var json = `{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "Ireland",
                    "short_name": "Ireland",
                    "types": [
                        "establishment",
                        "natural_feature"
                    ]
                }
            ],
            "formatted_address": "Ireland",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 55.38294149999999,
                        "lng": -5.431909999999999
                    },
                    "southwest": {
                        "lat": 51.4475448,
                         "lng": -10.4800237
                    }
                }
            }
        }
   ],
   "status" : "OK"
}`;
var data = JSON.parse(json);
var bounds = data.results[0].geometry.bounds;
var bound = bounds.northeast || bounds.southwest;
console.log(bound.lat, bound.lng);

If you know northeast is always an option you can simplify this to:

console.log(data.results[0].geometry.bounds.northeast);

That will give you your lat and lng.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda