-2

I am trying to fetch the attributes from the JSON.How to get the attribute url from the below JSON body?

I am receiving undefined in console
Here is my cloud function from

// shipmentai map tracking

exports.shipmentmaptracking = functions.https.onRequest((req, res) => {

  var request = require("request");
  var trackid = req.body.Tracking;


  let d = {
    "tracking_pages": [
      {
        "branded_tracking_theme_guid": "440bed90-617b-43ba-bd0e-31512ac66e23",
        "tracking_number": "9400111699000367101815",
        "carrier_code": "stamps_com",
        "service_code": "usps_priority_mail",

      }
    ]

  };


  var options = {
    method: 'POST',
    url: 'https://api.shipengine.com/v-beta/tracking_page',
    headers: {
      'Content-Type': 'application/json',
      'api-key': 'Wyo4gpVIXfElQSDgF9p/L9aQ9kX3Um60X8hRSo8VAes'
    },
    body: JSON.stringify(d),
  };

  console.log('Sending a ' + options.method + ' request to ' + options.url);

  request(options, function (error, response, body) {
    console.log('Successfully received a response from ShipEngine')

    if (error) {
      console.log('An error was returned: ' + error.message);
      res.status(500).send(error.message);
    }
    else if (response.statusCode >= 400) {
      console.log('An error was returned: ' + response.statusCode + ' ' + response.statusMessage);
      console.log(body);
      res.status(response.statusCode).send(body);
    }
    else {
      console.log('A successful response was returned');
      console.log(body);
      console.log(d.tracking_pages[0].url);
      //res.status(200).send({'URL':shippp.tracking_pages[0].url});
      console.log('statusCode:', response && response.statusCode);
    }
  });
});

Here is my Json output How to fetch the attribute URL from the JSON below

{
  "tracking_pages": [
    {
      "carrier_code": "stamps_com",
      "tracking_number": "9400111699000367101815",
      "branded_tracking_theme_guid": "440bed90-617b-43ba-bd0e-31512ac66e23",
      "token": "l1XKcsYaEECc903KqBvtaA",
      "url": "https://track.shipengine.com/se/v1/g/l1XKcsYaEECc903KqBvtaA",
      "service_code": "usps_priority_mail"
    }
  ],
  "page": 0,
  "pages": 0,
  "total": 0
}

Thanks in advance

lasya valiveti
  • 251
  • 3
  • 12
  • visit:https://stackoverflow.com/questions/2499567/how-to-make-a-json-call-to-a-url/2499647#2499647 should answer ur question – Atul Mathew Apr 29 '19 at 04:13

3 Answers3

2

Simply get it from the data - note that I have given the entire object a name ("data") and that tracking_pages is an array - so you have to use the index number to get at it (assuming that there will be morethan one - otherwise there is no need to use an array) - and then its just the url property within that.

data.tracking_pages[0].url;

Obviously you will need a bit more sophistication in the selction - I's assuming that the actual data is more complex. And you can use JSON.parse() to convert the json o a regualr object.

But its just as simple as traversing the parents in the object and getting the selector correct.

let data = {
      "tracking_pages": [
        {
          "carrier_code": "stamps_com",
          "tracking_number": "9400111699000367101815",
          "branded_tracking_theme_guid": "440bed90-617b-43ba-bd0e-31512ac66e23",
          "token": "FmUfsOmjdEuioBuen1lMVA",
          "url": "https://track.shipengine.com/se/v1/g/FmUfsOmjdEuioBuen1lMVA",
          "service_code": "usps_priority_mail"
        }
      ],
      "page": 0,
      "pages": 0,
      "total": 0
    }
    
    
 let trackingUrl = data.tracking_pages[0].url;
 
 
console.log(trackingUrl) // gives https://track.shipengine.com/se/v1/g/FmUfsOmjdEuioBuen1lMVA
gavgrif
  • 15,194
  • 2
  • 25
  • 27
1

You can use . notation to access the url property in the object

var a={
      "tracking_pages": [
        {
          "carrier_code": "stamps_com",
          "tracking_number": "9400111699000367101815",
          "branded_tracking_theme_guid": "440bed90-617b-43ba-bd0e-31512ac66e23",
          "token": "FmUfsOmjdEuioBuen1lMVA",
          "url": "https://track.shipengine.com/se/v1/g/FmUfsOmjdEuioBuen1lMVA",
          "service_code": "usps_priority_mail"
        }
      ],
      "page": 0,
      "pages": 0,
      "total": 0
    };
   
    console.log(a.tracking_pages[0].url)
ellipsis
  • 12,049
  • 2
  • 17
  • 33
1

The expected output is not clear. If tracking_pages contain multiple url then you can use map to return an array of urls

let da = {
  "tracking_pages": [{
    "carrier_code": "stamps_com",
    "tracking_number": "9400111699000367101815",
    "branded_tracking_theme_guid": "440bed90-617b-43ba-bd0e-31512ac66e23",
    "token": "FmUfsOmjdEuioBuen1lMVA",
    "url": "https://track.shipengine.com/se/v1/g/FmUfsOmjdEuioBuen1lMVA",
    "service_code": "usps_priority_mail"
  }],
  "page": 0,
  "pages": 0,
  "total": 0
}

let urlAr = da.tracking_pages.map(item => item.url);
console.log(urlAr)
brk
  • 48,835
  • 10
  • 56
  • 78