Trying to access JSON data through an API

Multi tool use
Multi tool use












0















I am using node request to access data on an api. Now when I use the following code to access the body I get the following response.



app.post("/", function(req, res){

//var series = req.body.series;
var circuits = req.body.Circuits;
var url = req.body.url

request("http://ergast.com/api/f1/circuits/" + circuits + ".json", function(error, response, body){
console.log(body);
res.write("<p>Here is the information about the circuit " + circuits + " in the " + url + " is about the circuit </p>")
});
})


and I get the following response from the Api.



{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/circuits/brands_hatch.json",
"limit": "30",
"offset": "0",
"total": "1",
"CircuitTable": {
"circuitId": "brands_hatch",
"Circuits": [
{
"circuitId": "brands_hatch",
"url": "http://en.wikipedia.org/wiki/Brands_Hatch",
"circuitName": "Brands Hatch",
"Location": {
"lat": "51.3569",
"long": "0.263056",
"locality": "Kent",
"country": "UK"
}
}
]
}
}
}


Now the question is that I need to access the properties. Like for instance, the url gives me an error of undefined in the paragraph tag.
Any help would be appreciated.










share|improve this question























  • body.MRData.url doesn't work?

    – Pedro Silva
    Dec 30 '18 at 20:21











  • @Pedro Silva That does not work. It still gives me undefined!

    – AltBrian
    Dec 30 '18 at 20:45











  • Check the typeof body like, console.log(typeof body), if it's string that you should use JSON.parse(body).MRData.url, if not I really can't understand your problem.

    – Pedro Silva
    Dec 30 '18 at 20:48











  • I am trying to access url or any of the properties in Circuits: . This is the response I am getting. TypeError: Cannot read property 'CircuitTable' of undefined

    – AltBrian
    Dec 30 '18 at 20:51
















0















I am using node request to access data on an api. Now when I use the following code to access the body I get the following response.



app.post("/", function(req, res){

//var series = req.body.series;
var circuits = req.body.Circuits;
var url = req.body.url

request("http://ergast.com/api/f1/circuits/" + circuits + ".json", function(error, response, body){
console.log(body);
res.write("<p>Here is the information about the circuit " + circuits + " in the " + url + " is about the circuit </p>")
});
})


and I get the following response from the Api.



{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/circuits/brands_hatch.json",
"limit": "30",
"offset": "0",
"total": "1",
"CircuitTable": {
"circuitId": "brands_hatch",
"Circuits": [
{
"circuitId": "brands_hatch",
"url": "http://en.wikipedia.org/wiki/Brands_Hatch",
"circuitName": "Brands Hatch",
"Location": {
"lat": "51.3569",
"long": "0.263056",
"locality": "Kent",
"country": "UK"
}
}
]
}
}
}


Now the question is that I need to access the properties. Like for instance, the url gives me an error of undefined in the paragraph tag.
Any help would be appreciated.










share|improve this question























  • body.MRData.url doesn't work?

    – Pedro Silva
    Dec 30 '18 at 20:21











  • @Pedro Silva That does not work. It still gives me undefined!

    – AltBrian
    Dec 30 '18 at 20:45











  • Check the typeof body like, console.log(typeof body), if it's string that you should use JSON.parse(body).MRData.url, if not I really can't understand your problem.

    – Pedro Silva
    Dec 30 '18 at 20:48











  • I am trying to access url or any of the properties in Circuits: . This is the response I am getting. TypeError: Cannot read property 'CircuitTable' of undefined

    – AltBrian
    Dec 30 '18 at 20:51














0












0








0








I am using node request to access data on an api. Now when I use the following code to access the body I get the following response.



app.post("/", function(req, res){

//var series = req.body.series;
var circuits = req.body.Circuits;
var url = req.body.url

request("http://ergast.com/api/f1/circuits/" + circuits + ".json", function(error, response, body){
console.log(body);
res.write("<p>Here is the information about the circuit " + circuits + " in the " + url + " is about the circuit </p>")
});
})


and I get the following response from the Api.



{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/circuits/brands_hatch.json",
"limit": "30",
"offset": "0",
"total": "1",
"CircuitTable": {
"circuitId": "brands_hatch",
"Circuits": [
{
"circuitId": "brands_hatch",
"url": "http://en.wikipedia.org/wiki/Brands_Hatch",
"circuitName": "Brands Hatch",
"Location": {
"lat": "51.3569",
"long": "0.263056",
"locality": "Kent",
"country": "UK"
}
}
]
}
}
}


Now the question is that I need to access the properties. Like for instance, the url gives me an error of undefined in the paragraph tag.
Any help would be appreciated.










share|improve this question














I am using node request to access data on an api. Now when I use the following code to access the body I get the following response.



app.post("/", function(req, res){

//var series = req.body.series;
var circuits = req.body.Circuits;
var url = req.body.url

request("http://ergast.com/api/f1/circuits/" + circuits + ".json", function(error, response, body){
console.log(body);
res.write("<p>Here is the information about the circuit " + circuits + " in the " + url + " is about the circuit </p>")
});
})


and I get the following response from the Api.



{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/circuits/brands_hatch.json",
"limit": "30",
"offset": "0",
"total": "1",
"CircuitTable": {
"circuitId": "brands_hatch",
"Circuits": [
{
"circuitId": "brands_hatch",
"url": "http://en.wikipedia.org/wiki/Brands_Hatch",
"circuitName": "Brands Hatch",
"Location": {
"lat": "51.3569",
"long": "0.263056",
"locality": "Kent",
"country": "UK"
}
}
]
}
}
}


Now the question is that I need to access the properties. Like for instance, the url gives me an error of undefined in the paragraph tag.
Any help would be appreciated.







node.js api npm request






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 20:17









AltBrianAltBrian

281319




281319













  • body.MRData.url doesn't work?

    – Pedro Silva
    Dec 30 '18 at 20:21











  • @Pedro Silva That does not work. It still gives me undefined!

    – AltBrian
    Dec 30 '18 at 20:45











  • Check the typeof body like, console.log(typeof body), if it's string that you should use JSON.parse(body).MRData.url, if not I really can't understand your problem.

    – Pedro Silva
    Dec 30 '18 at 20:48











  • I am trying to access url or any of the properties in Circuits: . This is the response I am getting. TypeError: Cannot read property 'CircuitTable' of undefined

    – AltBrian
    Dec 30 '18 at 20:51



















  • body.MRData.url doesn't work?

    – Pedro Silva
    Dec 30 '18 at 20:21











  • @Pedro Silva That does not work. It still gives me undefined!

    – AltBrian
    Dec 30 '18 at 20:45











  • Check the typeof body like, console.log(typeof body), if it's string that you should use JSON.parse(body).MRData.url, if not I really can't understand your problem.

    – Pedro Silva
    Dec 30 '18 at 20:48











  • I am trying to access url or any of the properties in Circuits: . This is the response I am getting. TypeError: Cannot read property 'CircuitTable' of undefined

    – AltBrian
    Dec 30 '18 at 20:51

















body.MRData.url doesn't work?

– Pedro Silva
Dec 30 '18 at 20:21





body.MRData.url doesn't work?

– Pedro Silva
Dec 30 '18 at 20:21













@Pedro Silva That does not work. It still gives me undefined!

– AltBrian
Dec 30 '18 at 20:45





@Pedro Silva That does not work. It still gives me undefined!

– AltBrian
Dec 30 '18 at 20:45













Check the typeof body like, console.log(typeof body), if it's string that you should use JSON.parse(body).MRData.url, if not I really can't understand your problem.

– Pedro Silva
Dec 30 '18 at 20:48





Check the typeof body like, console.log(typeof body), if it's string that you should use JSON.parse(body).MRData.url, if not I really can't understand your problem.

– Pedro Silva
Dec 30 '18 at 20:48













I am trying to access url or any of the properties in Circuits: . This is the response I am getting. TypeError: Cannot read property 'CircuitTable' of undefined

– AltBrian
Dec 30 '18 at 20:51





I am trying to access url or any of the properties in Circuits: . This is the response I am getting. TypeError: Cannot read property 'CircuitTable' of undefined

– AltBrian
Dec 30 '18 at 20:51












1 Answer
1






active

oldest

votes


















1














In your console.log it should be



console.log(JSON.parse(body)); 


Or



var bodyParsed = JSON.parse(body);
var yourUrl = bodyParsed.MRData.url


As I said in the comments, your body is a string, console.log(typeof body) returns string, so it needs to be parsed to an object with properties.






share|improve this answer
























  • Suppose I wanted to access the url in this part of the json data "Circuits": [ { "circuitId": "brands_hatch", "url": "http://en.wikipedia.org/wiki/Brands_Hatch", "circuitName": "Brands Hatch", "Location": { "lat": "51.3569", "long": "0.263056", "locality": "Kent", "country": "UK" }

    – AltBrian
    Dec 30 '18 at 21:06











  • bodyParsed.MRData.CircuitTable should work like you requested.

    – Pedro Silva
    Dec 30 '18 at 21:15











  • That does not work as it still says undefined.

    – AltBrian
    Dec 30 '18 at 21:38











  • var request = require('request'); request('ergast.com/api/f1/circuits/brands_hatch.json', function (error, response, body) { var bodyParsed = JSON.parse(body) console.log(bodyParsed.MRData.CircuitTable); }); That's my code, and it works like a charm, I don't understand how it can't work with you.

    – Pedro Silva
    Dec 30 '18 at 21:41













  • this is what I get with my paragraph tags Here is the information about the circuit aintree in the [object Object] series. res.write("<p>Here is the information about the circuit " + circuits + " in the " + yourUrl + " series </p>")

    – AltBrian
    Dec 30 '18 at 21:55











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981101%2ftrying-to-access-json-data-through-an-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In your console.log it should be



console.log(JSON.parse(body)); 


Or



var bodyParsed = JSON.parse(body);
var yourUrl = bodyParsed.MRData.url


As I said in the comments, your body is a string, console.log(typeof body) returns string, so it needs to be parsed to an object with properties.






share|improve this answer
























  • Suppose I wanted to access the url in this part of the json data "Circuits": [ { "circuitId": "brands_hatch", "url": "http://en.wikipedia.org/wiki/Brands_Hatch", "circuitName": "Brands Hatch", "Location": { "lat": "51.3569", "long": "0.263056", "locality": "Kent", "country": "UK" }

    – AltBrian
    Dec 30 '18 at 21:06











  • bodyParsed.MRData.CircuitTable should work like you requested.

    – Pedro Silva
    Dec 30 '18 at 21:15











  • That does not work as it still says undefined.

    – AltBrian
    Dec 30 '18 at 21:38











  • var request = require('request'); request('ergast.com/api/f1/circuits/brands_hatch.json', function (error, response, body) { var bodyParsed = JSON.parse(body) console.log(bodyParsed.MRData.CircuitTable); }); That's my code, and it works like a charm, I don't understand how it can't work with you.

    – Pedro Silva
    Dec 30 '18 at 21:41













  • this is what I get with my paragraph tags Here is the information about the circuit aintree in the [object Object] series. res.write("<p>Here is the information about the circuit " + circuits + " in the " + yourUrl + " series </p>")

    – AltBrian
    Dec 30 '18 at 21:55
















1














In your console.log it should be



console.log(JSON.parse(body)); 


Or



var bodyParsed = JSON.parse(body);
var yourUrl = bodyParsed.MRData.url


As I said in the comments, your body is a string, console.log(typeof body) returns string, so it needs to be parsed to an object with properties.






share|improve this answer
























  • Suppose I wanted to access the url in this part of the json data "Circuits": [ { "circuitId": "brands_hatch", "url": "http://en.wikipedia.org/wiki/Brands_Hatch", "circuitName": "Brands Hatch", "Location": { "lat": "51.3569", "long": "0.263056", "locality": "Kent", "country": "UK" }

    – AltBrian
    Dec 30 '18 at 21:06











  • bodyParsed.MRData.CircuitTable should work like you requested.

    – Pedro Silva
    Dec 30 '18 at 21:15











  • That does not work as it still says undefined.

    – AltBrian
    Dec 30 '18 at 21:38











  • var request = require('request'); request('ergast.com/api/f1/circuits/brands_hatch.json', function (error, response, body) { var bodyParsed = JSON.parse(body) console.log(bodyParsed.MRData.CircuitTable); }); That's my code, and it works like a charm, I don't understand how it can't work with you.

    – Pedro Silva
    Dec 30 '18 at 21:41













  • this is what I get with my paragraph tags Here is the information about the circuit aintree in the [object Object] series. res.write("<p>Here is the information about the circuit " + circuits + " in the " + yourUrl + " series </p>")

    – AltBrian
    Dec 30 '18 at 21:55














1












1








1







In your console.log it should be



console.log(JSON.parse(body)); 


Or



var bodyParsed = JSON.parse(body);
var yourUrl = bodyParsed.MRData.url


As I said in the comments, your body is a string, console.log(typeof body) returns string, so it needs to be parsed to an object with properties.






share|improve this answer













In your console.log it should be



console.log(JSON.parse(body)); 


Or



var bodyParsed = JSON.parse(body);
var yourUrl = bodyParsed.MRData.url


As I said in the comments, your body is a string, console.log(typeof body) returns string, so it needs to be parsed to an object with properties.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 30 '18 at 21:02









Pedro SilvaPedro Silva

706215




706215













  • Suppose I wanted to access the url in this part of the json data "Circuits": [ { "circuitId": "brands_hatch", "url": "http://en.wikipedia.org/wiki/Brands_Hatch", "circuitName": "Brands Hatch", "Location": { "lat": "51.3569", "long": "0.263056", "locality": "Kent", "country": "UK" }

    – AltBrian
    Dec 30 '18 at 21:06











  • bodyParsed.MRData.CircuitTable should work like you requested.

    – Pedro Silva
    Dec 30 '18 at 21:15











  • That does not work as it still says undefined.

    – AltBrian
    Dec 30 '18 at 21:38











  • var request = require('request'); request('ergast.com/api/f1/circuits/brands_hatch.json', function (error, response, body) { var bodyParsed = JSON.parse(body) console.log(bodyParsed.MRData.CircuitTable); }); That's my code, and it works like a charm, I don't understand how it can't work with you.

    – Pedro Silva
    Dec 30 '18 at 21:41













  • this is what I get with my paragraph tags Here is the information about the circuit aintree in the [object Object] series. res.write("<p>Here is the information about the circuit " + circuits + " in the " + yourUrl + " series </p>")

    – AltBrian
    Dec 30 '18 at 21:55



















  • Suppose I wanted to access the url in this part of the json data "Circuits": [ { "circuitId": "brands_hatch", "url": "http://en.wikipedia.org/wiki/Brands_Hatch", "circuitName": "Brands Hatch", "Location": { "lat": "51.3569", "long": "0.263056", "locality": "Kent", "country": "UK" }

    – AltBrian
    Dec 30 '18 at 21:06











  • bodyParsed.MRData.CircuitTable should work like you requested.

    – Pedro Silva
    Dec 30 '18 at 21:15











  • That does not work as it still says undefined.

    – AltBrian
    Dec 30 '18 at 21:38











  • var request = require('request'); request('ergast.com/api/f1/circuits/brands_hatch.json', function (error, response, body) { var bodyParsed = JSON.parse(body) console.log(bodyParsed.MRData.CircuitTable); }); That's my code, and it works like a charm, I don't understand how it can't work with you.

    – Pedro Silva
    Dec 30 '18 at 21:41













  • this is what I get with my paragraph tags Here is the information about the circuit aintree in the [object Object] series. res.write("<p>Here is the information about the circuit " + circuits + " in the " + yourUrl + " series </p>")

    – AltBrian
    Dec 30 '18 at 21:55

















Suppose I wanted to access the url in this part of the json data "Circuits": [ { "circuitId": "brands_hatch", "url": "http://en.wikipedia.org/wiki/Brands_Hatch", "circuitName": "Brands Hatch", "Location": { "lat": "51.3569", "long": "0.263056", "locality": "Kent", "country": "UK" }

– AltBrian
Dec 30 '18 at 21:06





Suppose I wanted to access the url in this part of the json data "Circuits": [ { "circuitId": "brands_hatch", "url": "http://en.wikipedia.org/wiki/Brands_Hatch", "circuitName": "Brands Hatch", "Location": { "lat": "51.3569", "long": "0.263056", "locality": "Kent", "country": "UK" }

– AltBrian
Dec 30 '18 at 21:06













bodyParsed.MRData.CircuitTable should work like you requested.

– Pedro Silva
Dec 30 '18 at 21:15





bodyParsed.MRData.CircuitTable should work like you requested.

– Pedro Silva
Dec 30 '18 at 21:15













That does not work as it still says undefined.

– AltBrian
Dec 30 '18 at 21:38





That does not work as it still says undefined.

– AltBrian
Dec 30 '18 at 21:38













var request = require('request'); request('ergast.com/api/f1/circuits/brands_hatch.json', function (error, response, body) { var bodyParsed = JSON.parse(body) console.log(bodyParsed.MRData.CircuitTable); }); That's my code, and it works like a charm, I don't understand how it can't work with you.

– Pedro Silva
Dec 30 '18 at 21:41







var request = require('request'); request('ergast.com/api/f1/circuits/brands_hatch.json', function (error, response, body) { var bodyParsed = JSON.parse(body) console.log(bodyParsed.MRData.CircuitTable); }); That's my code, and it works like a charm, I don't understand how it can't work with you.

– Pedro Silva
Dec 30 '18 at 21:41















this is what I get with my paragraph tags Here is the information about the circuit aintree in the [object Object] series. res.write("<p>Here is the information about the circuit " + circuits + " in the " + yourUrl + " series </p>")

– AltBrian
Dec 30 '18 at 21:55





this is what I get with my paragraph tags Here is the information about the circuit aintree in the [object Object] series. res.write("<p>Here is the information about the circuit " + circuits + " in the " + yourUrl + " series </p>")

– AltBrian
Dec 30 '18 at 21:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981101%2ftrying-to-access-json-data-through-an-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







oEAt,I6x0,AJYbqU
hgqVUSQDI,1

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas