Sample code for 30+ languages & platforms
DataFlex

Iterate over JSON Array containing JSON Objects

See more JSON Examples

Demonstrates how to load a JSON array and iterate over the JSON objects.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vSbJsonArray
    Handle hoSbJsonArray
    Handle hoArr
    Integer iTagId
    String sTagDescription
    Boolean iIsPublic
    Integer i
    Integer iCount
    Variant vObj
    Handle hoObj

    Move False To iSuccess

    // Loads the following JSON array and iterates over the objects:
    // 
    // [
    // {"tagId":95,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":98,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":101,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":104,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":107,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":110,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":113,"tagDescription":"hola 1","isPublic":true},
    // {"tagId":114,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":111,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":108,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":105,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":102,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":99,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":96,"tagDescription":"hola 2","isPublic":true},
    // {"tagId":97,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":100,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":103,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":106,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":109,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":112,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":115,"tagDescription":"hola 3","isPublic":true},
    // {"tagId":93,"tagDescription":"new tag","isPublic":true},
    // {"tagId":94,"tagDescription":"new tag","isPublic":true},
    // {"tagId":89,"tagDescription":"tag 1","isPublic":true},
    // {"tagId":90,"tagDescription":"tag 2","isPublic":true},
    // {"tagId":91,"tagDescription":"tag private 1","isPublic":false},
    // {"tagId":92,"tagDescription":"tag private 2","isPublic":false}
    // ]

    // Load a file containing the above JSON..
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJsonArray
    If (Not(IsComObjectCreated(hoSbJsonArray))) Begin
        Send CreateComObject of hoSbJsonArray
    End
    Get ComLoadFile Of hoSbJsonArray "qa_data/json/arraySample.json" "utf-8" To iSuccess

    Get Create (RefClass(cComChilkatJsonArray)) To hoArr
    If (Not(IsComObjectCreated(hoArr))) Begin
        Send CreateComObject of hoArr
    End
    Get pvComObject of hoSbJsonArray to vSbJsonArray
    Get ComLoadSb Of hoArr vSbJsonArray To iSuccess

    Move 0 To i
    Get ComSize Of hoArr To iCount

    While (i < iCount)
        Get ComObjectAt Of hoArr i To vObj
        If (IsComObject(vObj)) Begin
            Get Create (RefClass(cComChilkatJsonObject)) To hoObj
            Set pvComObject Of hoObj To vObj
        End
        Get ComIntOf Of hoObj "tagId" To iTagId
        Get ComStringOf Of hoObj "tagDescription" To sTagDescription
        Get ComBoolOf Of hoObj "isPublic" To iIsPublic

        Showln "tagId: " iTagId
        Showln "tagDescription: " sTagDescription
        Showln "isPublic: " iIsPublic
        Showln "--"

        Send Destroy of hoObj
        Move (i + 1) To i
    Loop



End_Procedure