0

I have a json file with an object with multiple nested arrays and objects and I have trouble finding a way to iterate through it and get all the data as properties are nested in various 'locations' inside the object. I tried to simplify the object and hope its not too large:

    {
        "trips": [{
            "trains": {
                "originTrains": {
                    "originTrain": [{
                        //always 2 x trainSegment - 1 for train to location and return train
                        "trainSegment": [{
                            //unknown numbers of objects inside trainSegment array
                            "Departure": {
                                "Location": "xxx"
                            },
                            "Arrival": {
                                "Location": "yyy"
                            },
                            "duration": 195,

                        }, {
                            "Departure": {
                                "Location": "yyy"
                            },
                            "Arrival": {
                                "Location": "zzz"
                            },
                            "duration": 240,
                        }],
                        "duration": 485
                    }, {
                        "trainSegment": [{
                            "Departure": {
                                "Location": "zzz"
                            },
                            "Arrival": {
                                "Location": "mmm"
                            },
                            "duration": 160,
                        }, {
                            "Departure": {
                                "Location": "mmm"
                            },
                            "Arrival": {
                                "Location": "xxx"
                            },
                            "duration": 325,
                        }],
                        "duration": 745
                    }]
                },
                "Direction": "Return"
            },

            "PriceInfo": {
                "Breakdown": {
                    "FareBreakdown": {
                        "PassengerQuantity": {
                            "Quantity": 1,
                            "Code": "ABC"
                        },
                        "Endorsements": {
                            "NotRefundable": true
                        }
                    }
                },
                "FareInfos": {
                    "FareInfo": [{
                        "Extensions": {
                            "SeatsRemaining": {
                                "BelowMinimum": false,
                                "Number": 4
                            }
                        }
                    }]
                },
                "Fare": {
                    "TotalFare": {
                        "CurrencyCode": "USD",
                        "DecimalPlaces": 2,
                        "Amount": 341.61
                    },
                    "Taxes": {
                        "Tax": [{
                            "CurrencyCode": "USD",
                            "DecimalPlaces": 2,
                            "TaxCode": "TOTALTAX",
                            "Amount": 66.25
                        }]
                    },
                    "BaseFare": {
                        "CurrencyCode": "USD",
                        "DecimalPlaces": 2,
                        "Amount": 275.36
                    }
                }
            },
            "TicketingInfo": {
                "TicketType": "eTicket"
            }
        }, {
            "trains": {
                "originTrains": {
                    //all over again


    }

Eventually I want to display all the info (as origin, destination,time, duration for all trains. Anything I tried so far didn't really work. I didn't provide my code as all my loops don't make sense and didn't work, but could provide of course. Any help would be very much appreciated. Thank you.

Dmitry
  • 6,716
  • 14
  • 37
  • 39
julicode
  • 3
  • 5
  • *I have trouble finding a way to iterate through it* What do you exactly want to achieve ? – Serge K. Oct 18 '17 at 12:06
  • You only show your JSON object, but what have you try? Please, share your code! – funcoding Oct 18 '17 at 12:08
  • What do you want as output ? – Faly Oct 18 '17 at 12:13
  • My advice to you is go step by step. output your data use the console.log to expand your output then you could see clearly what you need to display and the object structures. – funcoding Oct 18 '17 at 12:15
  • I need the data for pretty much all the properties I listed: ALL Departure, Arival, Time values for ALL trainSegment(s), Direction, TotalFare, Taxes – julicode Oct 18 '17 at 12:21
  • I tried to go step by step and console.log, its just whatever I tried was not working and then I posted it here in hope for some help as I am totally lost with this amount of data :-/ – julicode Oct 18 '17 at 12:22
  • Also, my problem is that I have constantly create nested loops. Like first I have to loop through trains.originTrains.originTrain, then inside I have to also iterate through trainSegment, and then I also have to iterate through all 'trains' as I need the info for all trains – julicode Oct 18 '17 at 12:29
  • You don't have to do that! Code in smaller pieces! Get the originTrains and create another function to get the values. It will make the code simple to read and debug! – funcoding Oct 18 '17 at 12:31
  • For example. (assuming your object is data) `data.trips.map(trip => { return trip.trains.originTrains; })` will return an array of all the originsTrains; – funcoding Oct 18 '17 at 12:36

0 Answers0