JSON Parsing Help

  1. 2 weeks ago

    Hi All,

    I'm try to pull data from the following JSON, I;m able to pull values from the top level and within the VideoMetadata branch such as "JobStatus" and "Codec" however I'm having trouble with the code to pull the data from the Labels, Label branch for the value contained in "Name". Below is the snip of the JSON and my code.

    Any help Appreciated.

    {
        "JobStatus": "SUCCEEDED",
        "VideoMetadata": {
            "Codec": "h264",
            "DurationMillis": 32270,
            "Format": "QuickTime / MOV",
            "FrameRate": 25.007747650146484,
            "FrameHeight": 1080,
            "FrameWidth": 1920
        },
        "NextToken": "EWz5UUwouYHy8wkHnfXKeA7pgBIj4j7GzHRfnibP8i9efv9oXV8RUeFKYK1d8Vj5PY/2gvkAvqPo",
        "Labels": [
            {
                "Timestamp": 0,
                "Label": {
                    "Name": "Adventure",
                    "Confidence": 50.74354934692383,
                    "Instances": [],
                    "Parents": [
                        {
                            "Name": "Leisure Activities"
                        }
                    ]
                }
            },
            {
                "Timestamp": 0,
                "Label": {
                    "Name": "Boat",
                    "Confidence": 96.60441589355469,
                    "Instances": [
                        {
                            "BoundingBox": {
                                "Width": 0.06501070410013199,
                                "Height": 0.166324645280838,
                                "Left": 0.41527944803237915,
                                "Top": 0.4101371467113495
                            },
                            "Confidence": 96.57678985595703
                        },
                        {
                            "BoundingBox": {
                                "Width": 0.07209968566894531,
                                "Height": 0.1852024793624878,
                                "Left": 0.5770155191421509,
                                "Top": 0.3925584554672241
                            },
                            "Confidence": 94.62499237060547
                        },
                        {
                            "BoundingBox": {
                                "Width": 0.05135205760598183,
                                "Height": 0.16055727005004883,
                                "Left": 0.542992889881134,
                                "Top": 0.419971764087677
                            },
                            "Confidence": 89.9786605834961
                        },
                        {
                            "BoundingBox": {
                                "Width": 0.07703933864831924,
                                "Height": 0.13837294280529022,
                                "Left": 0.8235676288604736,
                                "Top": 0.44508177042007446
                            },
                            "Confidence": 77.41576385498047
                        },
                        {
                            "BoundingBox": {
                                "Width": 0.08093808591365814,
                                "Height": 0.11271407455205917,
                                "Left": 0.7117553949356079,
                                "Top": 0.4803471267223358
                            },
                            "Confidence": 66.55744171142578
                        },
                        {
                            "BoundingBox": {
                                "Width": 0.052500661462545395,
                                "Height": 0.12892289459705353,
                                "Left": 0.9461998343467712,
                                "Top": 0.45804858207702637
                            },
                            "Confidence": 66.19795227050781
                        },
                        {
                            "BoundingBox": {
                                "Width": 0.04536215588450432,
                                "Height": 0.048764944076538086,
                                "Left": 0.9541170001029968,
                                "Top": 0.5467467904090881
                            },
                            "Confidence": 66.01787567138672
                        },
                        {
                            "BoundingBox": {
                                "Width": 0.06201972812414169,
                                "Height": 0.14266368746757507,
                                "Left": 0.8861428499221802,
                                "Top": 0.42416512966156006
                            },
                            "Confidence": 64.18550872802734
                        }
                    ],
                    "Parents": [
                        {
                            "Name": "Vehicle"
                        },
                        {
                            "Name": "Transportation"
                        }
                    ]
                }
            },
            {

    Xojo Code

    Dim jsonDict As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(s.Result.ToText)
    
    //Display the JobStatus
    Dim JobStatus As Text = jsonDict.Value("JobStatus")
    StatusTextField.text = JobStatus
    
    Dim VideoMetadata As Xojo.Core.Dictionary = jsonDict.Value("VideoMetadata")
    Dim Codec As Text = VideoMetadata.Value("Codec")
    CodecTextField.Text = Codec
    
    Dim Labels() As Auto = jsonDict.Value("Labels")
    For Each Label As Xojo.Core.Dictionary In Labels
      Dim Name As Text = Label.Value("Name")
      LabelTextArea.AppendText(EndOfLine + Name)
    Next

    Your For Each was just looping on the upper "Labels" part of the array, but the "Name" value is nested below "Labels" (plural) in another Dictionary which is "Label" (singular).

    Try the following:

    Dim jsonDict As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(t.ToText)
    
    //Display the JobStatus
    Dim JobStatus As Text = jsonDict.Value("JobStatus")
    StatusTextField.Text = JobStatus
    
    Dim VideoMetadata As Xojo.Core.Dictionary = jsonDict.Value("VideoMetadata")
    Dim Codec As Text = VideoMetadata.Value("Codec")
    CodecTextField.Text = Codec
    
    Dim Labels() As Auto = jsonDict.Value("Labels")
    If UBound(Labels) >= 0 Then // -1 means empty array, usually.
      For Each label_s_DICT As Xojo.Core.Dictionary In Labels
        Dim Timestamp As Integer = label_s_DICT.Value("Timestamp") // is a value
        Dim label_Dict As Xojo.Core.Dictionary = label_s_DICT.Value("Label") // is another nested Dictionary
        Dim Name As Text = label_Dict.Value("Name") // is a node of "label" Dictionary
        LabelTextArea.AppendText("Timestamp: " + Timestamp.ToText + " " + Name + EndOfLine)
      Next
    End If

    The above gave me an output of:

    Timestamp: 0 Adventure
    Timestamp: 0 Boat

    I hope that helps Evan.

  2. Kem T

    Sep 4 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    What is the trouble you're having specifically?

  3. Dale A

    Sep 4 San Diego, California, USA

    The "Labels" section is an array and therefore must be treated as such. That means that instead of a For Each loop you need to access the items in the array by index. I think something like the following (untested) should point you in the right direction.

    Dim Labels() As Auto = jsonDict.Value("Labels")
    If ubound(Labels) >= 0 Then // -1 means empty array, usually.
      Dim t1 As Xojo.Core.Dictionary = Labels(0)
      //  your For Each loop might go here to step through the values in "Label" section 
    End If

    Again, this is not final code so treat it as a guide only.

  4. 17 hours ago

    Thank you and apologies for the delayed response.

    I was able to progress a little further, however I'm still unable to determine how to parse the value for Name.

    Here's my latest code.

    Dim jsonDict As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(t.ToText)
    
    //Display the JobStatus
    Dim JobStatus As Text = jsonDict.Value("JobStatus")
    StatusTextField.text = JobStatus
    
    Dim VideoMetadata As Xojo.Core.Dictionary = jsonDict.Value("VideoMetadata")
    Dim Codec As Text = VideoMetadata.Value("Codec")
    CodecTextField.Text = Codec
    
    Dim Labels() As Auto = jsonDict.Value("Labels")
    If ubound(Labels) >= 0 Then // -1 means empty array, usually.
      Dim t1 As Xojo.Core.Dictionary = Labels(0)
      For Each Label As Xojo.Core.Dictionary In Labels
        Dim Timestamp As Integer = Label.Value("Timestamp")
        Dim Name As Text = Label.Value("Name")
        LabelTextArea.AppendText("Timestamp: " + Timestamp.ToText + " " + Name + EndofLine)
      Next
    End If

    I receive a KeyNotFoundException error, on the line Dim Name As Text = Label.Value("Name")

    Any ideas?

    Thanks again.

  5. 15 hours ago

    Scott C

    15 hours ago Pre-Release Testers, Xojo Pro Answer Vancouver, Canada

    Your For Each was just looping on the upper "Labels" part of the array, but the "Name" value is nested below "Labels" (plural) in another Dictionary which is "Label" (singular).

    Try the following:

    Dim jsonDict As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(t.ToText)
    
    //Display the JobStatus
    Dim JobStatus As Text = jsonDict.Value("JobStatus")
    StatusTextField.Text = JobStatus
    
    Dim VideoMetadata As Xojo.Core.Dictionary = jsonDict.Value("VideoMetadata")
    Dim Codec As Text = VideoMetadata.Value("Codec")
    CodecTextField.Text = Codec
    
    Dim Labels() As Auto = jsonDict.Value("Labels")
    If UBound(Labels) >= 0 Then // -1 means empty array, usually.
      For Each label_s_DICT As Xojo.Core.Dictionary In Labels
        Dim Timestamp As Integer = label_s_DICT.Value("Timestamp") // is a value
        Dim label_Dict As Xojo.Core.Dictionary = label_s_DICT.Value("Label") // is another nested Dictionary
        Dim Name As Text = label_Dict.Value("Name") // is a node of "label" Dictionary
        LabelTextArea.AppendText("Timestamp: " + Timestamp.ToText + " " + Name + EndOfLine)
      Next
    End If

    The above gave me an output of:

    Timestamp: 0 Adventure
    Timestamp: 0 Boat

    I hope that helps Evan.

  6. 14 hours ago

    Hi Scott, thank you, that worked perfectly. Really appreciate your help.

or Sign Up to reply!