Click here to Skip to main content
15,887,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need help on parsing a complex JSON. I've spent a lot of time searching the web to find solutions but I get more confused as I search. I don't usually work with JSON so I'm pretty novice.

In the JSON below, if someone could show me some code that would get me to the trainId field (about 10 lines from the bottom), I could probably figure out everything else from that since that would be one of the deepest levels of this JSON.

{
    "properties" : {
        "messageType" : "tripOrderRequest",
        "sentDateTime" : "2017-08-10T11:40:01.454Z",
        "senderId" : "1",
        "recipientId" : "2",
        "messageIntExt" : "external",
        "eventTypeVersion" : "1.1"
    },
    "body" : {
        "tripOrderId" : "d0b0f506-acbc-42f3-b712-bed7c60ff6b5",
        "shortKey" : "0ff6b5",
        "requestedVanHub" : "Oklahoma City, OK",
        "requestedVanHubID" : "1beff5b5-2cd3-41d3-9e34-f45a10717045",
        "tripOrderItinerary" : [ {
            "sequence" : 1,
            "type" : "P",
            "status" : "P",
            "associatedTripId" : "0bb3ab5d-9f08-488f-89f3-b07b072b6d51",
            "locationId" : "d78888a4-a6da-4c8c-b2d8-fbdd4b332394",
            "locationLat" : 36.116381,
            "locationLong" : -96.010583
        },{
            "sequence" : 2,
            "type" : "D",
            "status" : "P",
            "associatedTripId" : "0bb3ab5d-9f08-488f-89f3-b07b072b6d51",
            "locationId" : "c7016a4f-da4b-41d0-9f93-b38af1c36827",
            "locationLat" : 36.097914,
            "locationLong" : -95.865680
        } ],
        "tripDetails" : [ {
            "tripId" : "0bb3ab5d-9f08-488f-89f3-b07b072b6d51",
            "shortKey" : "2b6d51",
            "sequence" : 10,
            "pickupLocation" : {
                "adhoc" : {
                    "lat" : 36.116381,
                    "lng" : -96.010583         
                },
                "knownLocation" : {
                    "lat" : 36.116381,
                    "lng" : -96.010583,
                    "locationId" : "d78888a4-a6da-4c8c-b2d8-fbdd4b332394",
                    "name" : "Tulsa OK",
                    "address" : "1631 W 33RD PL, Tulsa, Oklahoma, 74107",
                    "radioChannel" : 51
                }
            },
            "dropOffLocation" : {
                "adhoc" : {
                    "lat" : 36.097914,
                    "lng" : -95.865680              
                },
                "knownLocation" : {
                    "lat" : 36.097914,
                    "lng" : -95.865680,
                    "locationId" : "c7016a4f-da4b-41d0-9f93-b38af1c36827",
                    "name" : "Tulsa - Cherokee YA OK",
                    "address" : "1631 W 33RD PL, Tulsa, Oklahoma, 74107",
                    "radioChannel" : 52
                }
            },
            "pickupSchedule" : {
                "preferredTimeZone" : "CDT",
                "needDateTime" : "2017-09-10T11:40:01.454Z",
                "pickupDateTime" : "2017-09-10T12:40:01.454Z"
            },
            "conveyance" : {
                "crew" : {
                    "crewMember" : [ {
                        "sequence" : 1,
                        "name" : "John Doe",
                        "id" : "x8888"
                    },{
                        "sequence" : 2,
                        "name" : "Johnny Doe",
                        "id" : "x8885"
                    } ],
                    "associatedTrainDetails" : {
                        "trainId" : "HPASLVJ113A",
                        "leadLocomotiveId" : "BNSF 2347"
                    }
                },
                "equipments" : [],
                "passenger" : []
            }
        } ] 
    }
}


What I have tried:

I have found ways to parse simple JSON using Newtonsoft.Json but can't seem to adapt it to get to deeper levels. I'm running short on a deadline to get this done.
Posted
Updated 22-Jun-18 4:53am
Comments
A_Griffin 22-Dec-17 8:49am    
Just wondering how you got on with this? I agree that the online help/documentation for dealing with complex JSON structures is lousy to non-existent - which is odd really, as there must be quite a demand for it. I finally figured this out, if you still need a hand...
Dennis Schank 22-Dec-17 10:51am    
David_Wimbley provided an answer for me below. Thanks.

I would take your json and use jsonutils[^]. This converts your complex JSON to classes that you can use to serialize/deserialize your JSON without trying to write your own parser. So once you get the classes you should be able to do something like Private pp As Properties = JsonConvert.DeserializeObject(Of Properties)(json)

That VB may not be exact syntax as I dont normally work with VB.
 
Share this answer
 
Comments
Dennis Schank 22-Dec-17 10:47am    
Perfect! Thanks for your help, David!
If the field name you are looking for is unique to the json string, then this is the easiest way I have found:
https://www.codeproject.com/Tips/1176910/Easy-JSON-Recursion-in-VB-NET-with-Nested-Levels?msg=5531222#xx5531222xx

I modified the original to go deeper if the field name you are looking for is listed in several nodes but only for one level. You would need to modify to get to the correct node.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900