Search code examples
jsonvb.netsabre

How to process sabre InstaFlight API JSON response


I connected to saber InstaFlight API and got a result in JSON. The output string is too long and I can get their values. But my way is taking lots of memory storage. I want a way that takes less memory storage and a dynamic way in VB.NET.

The code below is working fine with no error:

response2 = DirectCast(postReq.GetResponse(), HttpWebResponse)
reader2 = New StreamReader(response2.GetResponseStream())
postReq.ContentType = "application/json; charset=utf-8"
Dim ser1 As JObject = JObject.Parse(reader2.ReadToEnd())

I can get the values like this, with no error:

ElapseTime1 = ser1("PricedItineraries")(0)("AirItinerary")("OriginDestinationOptions")("OriginDestinationOption")(0)("ElapsedTime").Value(Of String)()

However, it gets to a point that you need hundreds of variables and this is not the right way.

An example of the output JSON:

{ "PricedItineraries": [ { "AirItinerary": { "OriginDestinationOptions": { "OriginDestinationOption": [ { "FlightSegment": [ { "DepartureAirport": { "LocationCode": "JFK" }, "ArrivalAirport": { "LocationCode": "LAS" }, "MarketingAirline": { "Code": "AS" }, "ArrivalTimeZone": { "GMTOffset": -7 }, "TPA_Extensions": { "eTicket": { "Ind": true } }, "StopQuantity": 0, "ElapsedTime": 344, "ResBookDesigCode": "R", "MarriageGrp": "O", "Equipment": { "AirEquipType": 320 }, "DepartureDateTime": "2017-07-07T09:30:00", "ArrivalDateTime": "2017-07-07T12:14:00", "FlightNumber": 1251, "OnTimePerformance": { "Percentage": 70 }, "OperatingAirline": { "CompanyShortName": "VIRGIN AMERICA", "FlightNumber": 251, "Code": "VX" }, "DepartureTimeZone": { "GMTOffset": -4 } }, { "DepartureAirport": { "LocationCode": "LAS" }, "ArrivalAirport": { "LocationCode": "LAX" }, "MarketingAirline": { "Code": "AS" }, "ArrivalTimeZone": { "GMTOffset": -7 }, "TPA_Extensions": { "eTicket": { "Ind": true } }, "StopQuantity": 0, "ElapsedTime": 85, "ResBookDesigCode": "R", "MarriageGrp": "O", "Equipment": { "AirEquipType": 320 }, "DepartureDateTime": "2017-07-07T14:45:00", "ArrivalDateTime": "2017-07-07T16:10:00", "FlightNumber": 1475, "OnTimePerformance": { "Percentage": 36 }, "OperatingAirline": { "CompanyShortName": "VIRGIN AMERICA", "FlightNumber": 475, "Code": "VX" }, "DepartureTimeZone": { "GMTOffset": -7 } } ], "ElapsedTime": 580 }, { "FlightSegment": [ { "DepartureAirport": { "LocationCode": "LAX" }, "ArrivalAirport": { "LocationCode": "LAS" }, "MarketingAirline": { "Code": "AS" }, "ArrivalTimeZone": { "GMTOffset": -7 }, "TPA_Extensions": { "eTicket": { "Ind": true } }, "StopQuantity": 0, "ElapsedTime": 71, "ResBookDesigCode": "R", "MarriageGrp": "O", "Equipment": { "AirEquipType": 320 }, "DepartureDateTime": "2017-07-08T17:00:00", "ArrivalDateTime": "2017-07-08T18:11:00", "FlightNumber": 1480, "OnTimePerformance": { "Percentage": 55 }, "OperatingAirline": { "CompanyShortName": "VIRGIN AMERICA", "FlightNumber": 480, "Code": "VX" }, "DepartureTimeZone": { "GMTOffset": -7 } }, { "DepartureAirport": { "LocationCode": "LAS" }, "ArrivalAirport": { "LocationCode": "JFK" }, "MarketingAirline": { "Code": "AS" }, "ArrivalTimeZone": { "GMTOffset": -4 }, "TPA_Extensions": { "eTicket": { "Ind": true } }, "StopQuantity": 0,


Solution

  • I think you need to create a model for this result. According to this JSON, you have to create a class model.

    If you have this JSON:

    {
    "dailyDealId": "432",
    "discountPercentage": "0",
    "product": {
        "productId": "10",
        "brandId": "10",
        "departmentId": "3",
        "name": "Baby Girl Velour Tunic & Snowflake Legging Set",
        "description": "The pretty set",
        "url": "http://whatever.whatever.com/files/whatever.tif"
    }
    

    You need this model:

    public class Product
    {
        public string productId { get; set; }
        public string brandId { get; set; }
        public string departmentId { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string url { get; set; }
    }
    
    public class Data
    {
        public string dailyDealId { get; set; }
        public string discountPercentage { get; set; }
        public Product product { get; set; }
    }