0

Estoy intentando acceder a un JSON a través de un enlace, cuando imprimo en consola los datos se ven correctamente, pero cuando intento acceder a los datos individuales del JSON no puedo, ni como objetos, ni array ni string, intente un JSON.parse() que me da un error de sintaxis y un JSON.stringfy() que me devuelve ""

Este es el código

const https = require('https');

https.get('https://api.flightplandatabase.com/nav/airport/LEMD', res => {
            console.log(res.statusCode);
            console.log(res.headers);

            var body = '';

            res.on('data', data => {
                body += data;
            })


            res.on('end', () => console.log(body));

console.log(body) me devuelve lo siguiente

{"ICAO":"LEMD","IATA":"MAD","name":"Adolfo Suarez Madrid - Barajas","regionName":"Spain","elevation":1997.9986907010002,"lat":40.4954,"lon":-3.56021,"magneticVariation":-0.5163631015629957,"timezone":{"name":"Europe/Madrid","offset":7200},"times":{"sunrise":"2019-05-13T05:01:29.375Z","sunset":"2019-05-13T19:22:24.129Z","dawn":"2019-05-13T04:30:46.439Z","dusk":"2019-05-13T19:53:07.065Z"},"runwayCount":4,"runways":[{"ident":"14L","width":197.01443599499999,"length":11493.077445291001,"bearing":142.33,"surface":"ASPHALT","markings":["APP"],"lighting":["CENTERLINE","EDGE","THRESHOLD"],"thresholdOffset":0,"overrunLength":944.8818912,"ends":[{"ident":"14L","lat":40.4949,"lon":-3.55787},{"ident":"32R","lat":40.4949,"lon":40.47}],"navaids":[]},{"ident":"14R","width":197.01443599499999,"length":13090.255925409,"bearing":142.299,"surface":"ASPHALT","markings":["APP"],"lighting":["CENTERLINE","EDGE","THRESHOLD"],"thresholdOffset":0,"overrunLength":583.9895022000001,"ends":[{"ident":"14R","lat":40.4849,"lon":-3.57602},{"ident":"32L","lat":40.4849,"lon":40.4565}],"navaids":[]},{"ident":"18L","width":197.01443599499999,"length":11685.793981017001,"bearing":179.75,"surface":"ASPHALT","markings":["PREC-APP"],"lighting":["CENTERLINE","EDGE","THRESHOLD","ALSF-II","TDZ","REIL-UNI"],"thresholdOffset":1843.8320238000001,"overrunLength":780.8398962,"ends":[{"ident":"18L","lat":40.5332,"lon":-3.55938},{"ident":"36R","lat":40.5332,"lon":40.5012}],"navaids":[{"ident":"IML","type":"GS","lat":40.5254,"lon":-3.55804,"airport":309570,"runway":"18L","frequency":111500000,"slope":3.0018,"bearing":180.518,"name":null,"elevation":1997.9986907010002,"range":9.999999991560001},{"ident":"IML","type":"LOC-ILS","lat":40.4984,"lon":-3.55919,"airport":309570,"runway":"18L","frequency":111500000,"slope":null,"bearing":179.912,"name":"ILS-cat-III","elevation":1997.9986907010002,"range":17.999999984808},{"ident":"IML","type":"DME","lat":40.5254,"lon":-3.55822,"airport":309570,"runway":"18L","frequency":111500000,"slope":null,"bearing":null,"name":"DME-ILS","elevation":1916.0006590803,"range":17.999999984808}]},{"ident":"18R","width":197.01443599499999,"length":13780.610257167,"bearing":179.75,"surface":"ASPHALT","markings":["PREC-APP"],"lighting":["CENTERLINE","EDGE","THRESHOLD","ALSF-II","TDZ","REIL-UNI"],"thresholdOffset":3225.0656217,"overrunLength":843.1758543000001,"ends":[{"ident":"18R","lat":40.5303,"lon":-3.57484},{"ident":"36L","lat":40.5303,"lon":40.4926}],"navaids":[{"ident":"IMR","type":"LOC-ILS","lat":40.4902,"lon":-3.57461,"airport":309570,"runway":"18R","frequency":110700000,"slope":null,"bearing":179.803,"name":"ILS-cat-III","elevation":1981.0006591791,"range":17.999999984808},{"ident":"IMR","type":"DME","lat":40.52,"lon":-3.57384,"airport":309570,"runway":"18R","frequency":110700000,"slope":null,"bearing":null,"name":"DME-ILS","elevation":1986.0006591867,"range":17.999999984808},{"ident":"IMR","type":"GS","lat":40.5199,"lon":-3.57337,"airport":309570,"runway":"18R","frequency":110700000,"slope":3.0018,"bearing":180.52,"name":null,"elevation":1977.9986906706001,"range":9.999999991560001}]},{"ident":"32L","width":197.01443599499999,"length":13090.255925409,"bearing":322.318,"surface":"ASPHALT","markings":["PREC-APP"],"lighting":["CENTERLINE","EDGE","THRESHOLD","ALSF-II","TDZ","REIL-UNI"],"thresholdOffset":3038.0577474,"overrunLength":400.2624678,"ends":[{"ident":"32L","lat":40.4565,"lon":-3.54719},{"ident":"14R","lat":40.4565,"lon":40.4849}],"navaids":[{"ident":"MAA","type":"GS","lat":40.4659,"lon":-3.55488,"airport":309570,"runway":"32L","frequency":109900000,"slope":3.00322,"bearing":322,"name":null,"elevation":1997.9986907010002,"range":9.999999991560001},{"ident":"MAA","type":"LOC-ILS","lat":40.4862,"lon":-3.57739,"airport":309570,"runway":"32L","frequency":109900000,"slope":null,"bearing":322.304,"name":"ILS-cat-III","elevation":1997.9986907010002,"range":17.999999984808},{"ident":"MAA","type":"DME","lat":40.4657,"lon":-3.55514,"airport":309570,"runway":"32L","frequency":109900000,"slope":null,"bearing":null,"name":"DME-ILS","elevation":1997.9986907010002,"range":17.999999984808},{"ident":"OM","type":"OM","lat":40.4008,"lon":-3.49064,"airport":309570,"runway":"32L","frequency":null,"slope":null,"bearing":322.318,"name":null,"elevation":1997.9986907010002,"range":0}]},{"ident":"32R","width":197.01443599499999,"length":11493.077445291001,"bearing":322.347,"surface":"ASPHALT","markings":["PREC-APP"],"lighting":["CENTERLINE","EDGE","THRESHOLD","ALSF-II","TDZ","REIL-UNI"],"thresholdOffset":1640.41995,"overrunLength":990.8136498,"ends":[{"ident":"32R","lat":40.47,"lon":-3.53257},{"ident":"14L","lat":40.47,"lon":40.4949}],"navaids":[{"ident":"MBB","type":"DME","lat":40.4762,"lon":-3.53728,"airport":309570,"runway":"32R","frequency":109100000,"slope":null,"bearing":null,"name":"DME-ILS","elevation":1997.9986907010002,"range":17.999999984808},{"ident":"MBB","type":"GS","lat":40.4762,"lon":-3.53713,"airport":309570,"runway":"32R","frequency":109100000,"slope":3.0032,"bearing":320,"name":null,"elevation":1997.9986907010002,"range":9.999999991560001},{"ident":"MBB","type":"LOC-ILS","lat":40.4971,"lon":-3.56022,"airport":309570,"runway":"32R","frequency":109100000,"slope":null,"bearing":322.308,"name":"ILS-cat-III","elevation":1997.9986907010002,"range":17.999999984808}]},{"ident":"36L","width":197.01443599499999,"length":13780.610257167,"bearing":359.75,"surface":"ASPHALT","markings":["APP"],"lighting":["CENTERLINE","EDGE","THRESHOLD"],"thresholdOffset":0,"overrunLength":0,"ends":[{"ident":"36L","lat":40.4926,"lon":-3.57462},{"ident":"18R","lat":40.4926,"lon":40.5303}],"navaids":[]},{"ident":"36R","width":197.01443599499999,"length":11685.793981017001,"bearing":359.75,"surface":"ASPHALT","markings":["APP"],"lighting":["CENTERLINE","EDGE","THRESHOLD"],"thresholdOffset":0,"overrunLength":472.4409456,"ends":[{"ident":"36R","lat":40.5012,"lon":-3.5592},{"ident":"18L","lat":40.5012,"lon":40.5332}],"navaids":[]}],"frequencies":[{"type":"DEP","frequency":131170000,"name":"Madrid Departure"},{"type":"APP","frequency":128700000,"name":"Madrid Approach"},{"type":"TWR","frequency":120150000,"name":"Barajas Tower"},{"type":"GND","frequency":123270000,"name":"Barajas Apron"},{"type":"REC","frequency":130850000,"name":"Madrid ATIS"},{"type":"APP","frequency":134950000,"name":"Madrid Approach"},{"type":"TWR","frequency":118070000,"name":"Barajas Tower"},{"type":"CLD","frequency":121070000,"name":"Madrid Clearance Delivery"},{"type":"TWR","frequency":120650000,"name":"Barajas Tower"},{"type":"TWR","frequency":118150000,"name":"Barajas Tower"},{"type":"GND","frequency":121700000,"name":"Barajas Apron"},{"type":"APP","frequency":118400000,"name":"Madrid Approach"},{"type":"APP","frequency":136100000,"name":"Madrid Approach"},{"type":"APP","frequency":127050000,"name":"Madrid Final"},{"type":"GND","frequency":121850000,"name":"Barajas Apron"},{"type":"APP","frequency":118750000,"name":"Madrid Approach"},{"type":"GND","frequency":123000000,"name":"Barajas Apron"},{"type":"TWR","frequency":118670000,"name":"Barajas Tower"},{"type":"GND","frequency":123250000,"name":"Barajas Apron"},{"type":"DEP","frequency":124230000,"name":"Madrid Departure"},{"type":"TWR","frequency":118970000,"name":"Barajas Tower"},{"type":"APP","frequency":124030000,"name":"Madrid Approach"}],"weather":{"METAR":"LEMD 130300Z VRB02KT CAVOK 11/M02 Q1022 NOSIG","TAF":"TAF LEMD 122300Z 1300/1406 04007KT CAVOK TX30/1316Z TN09/1306Z"}}

Solucionado agregando el JSON.parse() en la ultima función

           res.on('end', () => {
                var obj = JSON.parse(body)
                console.log(obj)
            });

Muchas gracias

6
  • Por favor, copia y pega como texto exactamente lo que te muestra console.log( body ). Sin saber eso, es imposible ayudarte. Un saludo.
    – Trauma
    el 13 may. 2019 a las 3:00
  • El JSON que has puesto tiene 2 saltos de línea. Si son reales, no es un JSON válido. Sin son consecuencia del copy/paste, es un JSON válido y tu problema está al acceder a ellos. Dices que te da un error de sintaxis ... así que es muy posible que ese JSON sea incorrecto.
    – Trauma
    el 13 may. 2019 a las 3:16
  • Parece que los saltos de linea han sido al copiar y pegar, lo he vuelto a hacer y no salen saltos de linea (parece)
    – Albert G.
    el 13 may. 2019 a las 3:21
  • Pues entonces es un JSON totalmente válido. ¿ Como estás intentando acceder a los elementos ? Porque un JSON.parse( ) funciona perfectamente.
    – Trauma
    el 13 may. 2019 a las 3:22
  • Efectivamente JSON.parse() funciona, el problema de sintaxis venia al no hacerlo en la ultima función, gracias por la ayuda
    – Albert G.
    el 13 may. 2019 a las 3:28

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.