B4J Question Json Tree parsing code is not usable for this dataset

David Kenny

Member
Licensed User
Longtime User
I'm a Json noob and have not been able to figure out how to parse this data. The Json Tree tool looks good for the first three lines, but then hard-codes the rest of it. The object names I am after are serial numbers and are never used twice. So the tool hard-coding them doesn't work. I've tried to use code from Erel's examples to write my own parser for this Json file, but have not made much progress. I know I could use Regex to parse it but have seen Erel tell other so not use Regex for Json files.

JSON:
{"U2730336": {"software_name": "radiosonde_auto_rx", "software_version": "1.5.10", "uploader_callsign": "TFDHU2", "uploader_position": "47.189994,19.780331", "uploader_antenna": "Sirio CX 395", "time_received": "2022-08-07T13:35:14.247058Z", "datetime": "2022-08-07T13:35:30.000000Z", "manufacturer": "Vaisala", "type": "RS41", "serial": "U2730336", "subtype": "RS41-SGP", "frame": 9952, "lat": 46.45059, "lon": 20.32032, "alt": 1343.02549, "temp": 19.2, "humidity": 60.0, "pressure": 874.61, "vel_v": -8.03833, "vel_h": 6.13774, "heading": 208.28209, "sats": 9, "batt": 2.7, "frequency": 402.301, "burst_timer": 27521, "snr": 9.8, "tx_frequency": 402.3, "user-agent": "Amazon CloudFront", "position": "46.45059,20.32032", "upload_time_delta": -1.602, "uploader_alt": 100.0}, "U2730335": {"software_name": "radiosonde_auto_rx", "software_version": "1.5.1", "uploader_callsign": "YO2MAB_AUTO_RX", "uploader_position": "46.197232,21.299073", "uploader_antenna": "GP", "time_received": "2022-08-07T23:47:35.280296Z", "datetime": "2022-08-07T23:47:51.000000Z", "manufacturer": "Vaisala", "type": "RS41", "serial": "U2730335", "subtype": "RS41-SGP", "frame": 3498, "lat": 46.3197, "lon": 20.19715, "alt": 12791.11911, "temp": -53.7, "humidity": 6.0, "pressure": 185.03, "vel_v": 5.25389, "vel_h": 18.2257, "heading": 52.57049, "sats": 10, "batt": 2.7, "frequency": 402.3, "burst_timer": 65535, "snr": 20.2, "user-agent": "Amazon CloudFront", "position": "46.3197,20.19715", "upload_time_delta": -1.307, "uploader_alt": 110.0}, "U2730253": {"software_name": "radiosonde_auto_rx", "software_version": "1.5.10", "uploader_callsign": "TFDHU2", "uploader_position": "47.189994,19.780331", "uploader_antenna": "Sirio CX 395", "time_received": "2022-08-07T01:16:39.240865Z", "datetime": "2022-08-07T01:16:55.000000Z", "manufacturer": "Vaisala", "type": "RS41", "serial": "U2730253", "subtype": "RS41-SGP", "frame": 8840, "lat": 46.27371, "lon": 20.13337, "alt": 1762.31789, "temp": 17.8, "humidity": 56.4, "pressure": 832.03, "vel_v": -11.62223, "vel_h": 6.94295, "heading": 201.94974, "sats": 10, "batt": 2.6, "frequency": 402.301, "burst_timer": 28574, "snr": 7.3, "tx_frequency": 402.3, "user-agent": "Amazon CloudFront", "position": "46.27371,20.13337", "upload_time_delta": -1.783, "uploader_alt": 100.0}, "U2430312": {"software_name": "rdzTTGOsonde", "software_version": "master_v0.9.1", "uploader_callsign": "M7CWL_RDZTTGO", "time_received": "2022-08-07T13:45:18.000Z", "manufacturer": "Vaisala", "serial": "U2430312", "datetime": "2022-08-07T13:45:35.000Z", "lat": 50.13155, "lon": -5.33795, "alt": 162.7114, "frequency": 403.3, "vel_h": 6.17718, "vel_v": -10.34904, "heading": 172.61783, "rssi": -118.5, "frame": 10299, "type": "RS41", "sats": 10, "subtype": "RS41-SG", "temp": 19.5, "humidity": 47.9, "burst_timer": 28636, "batt": 2.7, "uploader_antenna": "1/4 Wave Ground Plane", "uploader_position": "50.1874,-5.37056", "user-agent": "Amazon CloudFront", "position": "50.13155,-5.33795", "upload_time_delta": -19.193, "uploader_alt": 80}}

This is a very small subset of the downloaded data. This subset contains only 4 objects while today, for instance, there are over 400 and that changes throughout the day. The rest of the data is not really needed for showing the problem I am having. You can use this link to download the whole set if you are curious: https://api.v2.sondehub.org/sondes

Any help is appreciated, David
 

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
Public Sub TestJson2
    Dim LinkURL As String = $"https://api.v2.sondehub.org/sondes"$
    Wait For (GetURL(LinkURL)) Complete (DataResult As String)
    
    Dim Data As Map = DataResult.As(JSON).ToMap

    For Each k As String In Data.Keys
        Dim serial As String = Data.Get(k).As(Map).Get("serial")
        Log($"Code: ${k} - Serial: ${serial}"$)
    Next
End Sub

Public Sub GetURL(URL As String) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download(URL)
        j.GetRequest.SetHeader("Content-Type","application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = j.GetString
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
    Return ResultURL
End Sub

1660604170381.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
note: single or all records
B4X:
Public Sub TestJson2
    Dim LinkURL As String = $"https://api.v2.sondehub.org/sondes"$
    Wait For (GetURL(LinkURL)) Complete (DataResult As String)
    
    Dim Data As Map = DataResult.As(JSON).ToMap

    'if you want to access data from a single record
    Dim id As String = "22010357"
    Log(Data.Get(id).As(Map).Get("serial"))
    
    'list all records
    For Each k As String In Data.Keys
        Dim serial As String = Data.Get(k).As(Map).Get("serial")
        Log($"Code: ${k} - Serial: ${serial}"$)
    Next
    

End Sub
 
Upvote 1

David Kenny

Member
Licensed User
Longtime User
I never had a need for casting, or so I thought. That works great!

Thanks so much Omar! I really appreciate you taking the time to help me out.
 
Upvote 0
Top