Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a specific part from JSON response with Java in Spring Boot?

I am trying to get the current status of a flight from a 3rd party API. I just need very few fields from the JSON. If I do it in the traditional way I would have to create many classes. And, map the json to the classes. Is there a better way of doing it. In the JSON response,I just need the information in the FlightStatuses field.

{
    "request": {
        "airline": {
            "fsCode": "AA",
            "requestedCode": "AA"
        },
        "flight": {
            "requested": "100",
            "interpreted": "100"
        },
        "utc": {
            "requested": "false",
            "interpreted": false
        },
        "url": "https://api.flightstats.com/flex/flightstatus/rest/v2/json/flight/status/AA/100/dep/2019/10/1?utc=false",
        "nonstopOnly": {
            "interpreted": false
        },
        "date": {
            "year": "2019",
            "month": "10",
            "day": "1",
            "interpreted": "2019-10-01"
        }
    },
    "appendix": {
        "airlines": [
            {
                "fs": "AA",
                "iata": "AA",
                "icao": "AAL",
                "name": "American Airlines",
                "phoneNumber": "08457-567-567",
                "active": true
            },
            {
                "fs": "AY",
                "iata": "AY",
                "icao": "FIN",
                "name": "Finnair",
                "phoneNumber": "+ 358 600 140 140",
                "active": true
            },
            {
                "fs": "IB",
                "iata": "IB",
                "icao": "IBE",
                "name": "Iberia",
                "phoneNumber": "1800 772 4642",
                "active": true
            },
            {
                "fs": "LY",
                "iata": "LY",
                "icao": "ELY",
                "name": "El Al",
                "phoneNumber": "+ 972-3-9771111",
                "active": true
            },
            {
                "fs": "BA",
                "iata": "BA",
                "icao": "BAW",
                "name": "British Airways",
                "phoneNumber": "1-800-AIRWAYS",
                "active": true
            },
            {
                "fs": "GF",
                "iata": "GF",
                "icao": "GFA",
                "name": "Gulf Air",
                "phoneNumber": "973 17 335 777",
                "active": true
            }
        ],
        "airports": [
            {
                "fs": "LHR",
                "iata": "LHR",
                "icao": "EGLL",
                "faa": "",
                "name": "London Heathrow Airport",
                "city": "London",
                "cityCode": "LON",
                "stateCode": "EN",
                "countryCode": "GB",
                "countryName": "United Kingdom",
                "regionName": "Europe",
                "timeZoneRegionName": "Europe/London",
                "weatherZone": "",
                "localTime": "2019-10-01T15:25:25.492",
                "utcOffsetHours": 1.0,
                "latitude": 51.469603,
                "longitude": -0.453566,
                "elevationFeet": 80,
                "classification": 1,
                "active": true,
                "weatherUrl": "https://api.flightstats.com/flex/weather/rest/v1/json/all/LHR?codeType=fs",
                "delayIndexUrl": "https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/LHR?codeType=fs"
            },
            {
                "fs": "JFK",
                "iata": "JFK",
                "icao": "KJFK",
                "faa": "JFK",
                "name": "John F. Kennedy International Airport",
                "street1": "JFK Airport",
                "city": "New York",
                "cityCode": "NYC",
                "stateCode": "NY",
                "postalCode": "11430",
                "countryCode": "US",
                "countryName": "United States",
                "regionName": "North America",
                "timeZoneRegionName": "America/New_York",
                "weatherZone": "NYZ178",
                "localTime": "2019-10-01T10:25:25.493",
                "utcOffsetHours": -4.0,
                "latitude": 40.642335,
                "longitude": -73.78817,
                "elevationFeet": 13,
                "classification": 1,
                "active": true,
                "weatherUrl": "https://api.flightstats.com/flex/weather/rest/v1/json/all/JFK?codeType=fs",
                "delayIndexUrl": "https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/JFK?codeType=fs"
            }
        ],
        "equipments": [
            {
                "iata": "77W",
                "name": "Boeing 777-300ER",
                "turboProp": false,
                "jet": true,
                "widebody": true,
                "regional": false
            }
        ]
    },
    "flightStatuses": [
        {
            "flightId": 1016157813,
            "carrierFsCode": "AA",
            "flightNumber": "100",
            "departureAirportFsCode": "JFK",
            "arrivalAirportFsCode": "LHR",
            "departureDate": {
                "dateUtc": "2019-10-01T22:15:00.000Z",
                "dateLocal": "2019-10-01T18:15:00.000"
            },
            "arrivalDate": {
                "dateUtc": "2019-10-02T05:20:00.000Z",
                "dateLocal": "2019-10-02T06:20:00.000"
            },
            "status": "S",
            "schedule": {
                "flightType": "J",
                "serviceClasses": "RFJY",
                "restrictions": "",
                "uplines": [],
                "downlines": []
            },
            "operationalTimes": {
                "publishedDeparture": {
                    "dateUtc": "2019-10-01T22:15:00.000Z",
                    "dateLocal": "2019-10-01T18:15:00.000"
                },
                "scheduledGateDeparture": {
                    "dateUtc": "2019-10-01T22:15:00.000Z",
                    "dateLocal": "2019-10-01T18:15:00.000"
                },
                "estimatedGateDeparture": {
                    "dateUtc": "2019-10-01T22:15:00.000Z",
                    "dateLocal": "2019-10-01T18:15:00.000"
                },
                "publishedArrival": {
                    "dateUtc": "2019-10-02T05:20:00.000Z",
                    "dateLocal": "2019-10-02T06:20:00.000"
                },
                "scheduledGateArrival": {
                    "dateUtc": "2019-10-02T05:20:00.000Z",
                    "dateLocal": "2019-10-02T06:20:00.000"
                },
                "estimatedGateArrival": {
                    "dateUtc": "2019-10-02T05:20:00.000Z",
                    "dateLocal": "2019-10-02T06:20:00.000"
                }
            },
            "codeshares": [
                {
                    "fsCode": "AY",
                    "flightNumber": "4012",
                    "relationship": "L"
                },
                {
                    "fsCode": "BA",
                    "flightNumber": "1511",
                    "relationship": "L"
                },
                {
                    "fsCode": "GF",
                    "flightNumber": "6654",
                    "relationship": "L"
                },
                {
                    "fsCode": "IB",
                    "flightNumber": "4218",
                    "relationship": "L"
                },
                {
                    "fsCode": "LY",
                    "flightNumber": "8051",
                    "relationship": "L"
                }
            ],
            "delays": {},
            "flightDurations": {
                "scheduledBlockMinutes": 425
            },
            "airportResources": {
                "departureTerminal": "8",
                "departureGate": "16",
                "arrivalTerminal": "3"
            },
            "flightEquipment": {
                "scheduledEquipmentIataCode": "77W",
                "actualEquipmentIataCode": "77W",
                "tailNumber": "N730AN"
            }
        }
    ]
}
like image 465
Saiteja Konda Avatar asked Oct 02 '19 01:10

Saiteja Konda


1 Answers

You can parse your JSON as JsonNode.

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree("[your JSON here]");

After that get only fields, which you need. For example:

jsonNode.get("request").get("airline").get("requestedCode").asText()
like image 117
Dmytro Barsukov Avatar answered Nov 13 '22 03:11

Dmytro Barsukov