Sample code for 30+ languages & platforms
DataFlex

Modify Parts of JSON Document

See more JSON Examples

Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:
{
   "fruit": [
      	{
         "kind": "apple",
	 "count": 24,
	 "fresh": true,
	 "extraInfo": null,
	 "listA": [ "abc", 1, null, false ],
	 "objectB": { "animal" : "monkey" }
      	},
	{
         "kind": "pear",
	 "count": 18,
	 "fresh": false,
	 "extraInfo": null
	 "listA": [ "xyz", 24, null, true ],
	 "objectB": { "animal" : "lemur" }
	}
    ],
    "list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
    "alien" : true
}

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJson
    Variant vListA
    Handle hoListA
    Boolean iBNull
    Variant vTickerObj
    Handle hoTickerObj
    Variant vAa
    Handle hoAa
    Variant vAFruit
    Handle hoAFruit
    Variant vAppleObj
    Handle hoAppleObj
    Variant vPearObj
    Handle hoPearObj
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End

    // Load the JSON from a file.
    Get ComLoadFile Of hoJson "qa_data/json/modifySample.json" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJson To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // This example will not check for errors (i.e. null / false / 0 return values)...

    // Get the "list" array:

    Get Create (RefClass(cComChilkatJsonArray)) To hoListA
    If (Not(IsComObjectCreated(hoListA))) Begin
        Send CreateComObject of hoListA
    End

    Get pvComObject of hoListA to vListA
    Get ComArrayOf2 Of hoJson "list" vListA To iSuccess

    // Modify values in the list.

    // Change banana to plantain
    Get ComSetStringAt Of hoListA 0 "plantain" To iSuccess

    // Change 12 to 24
    Get ComSetIntAt Of hoListA 1 24 To iSuccess

    // Change true to false
    Get ComSetBoolAt Of hoListA 2 False To iSuccess

    // Is the 3rd item null?
    Get ComIsNullAt Of hoListA 3 To iBNull

    // Change "orange" to 32.
    Get ComSetIntAt Of hoListA 4 32 To iSuccess

    // Change 12.5 to 31.2
    Get ComSetNumberAt Of hoListA 5 "31.2" To iSuccess

    // Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
    // Do this by deleting, then inserting a new object at the same location.
    Get ComDeleteAt Of hoListA 6 To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoTickerObj
    If (Not(IsComObjectCreated(hoTickerObj))) Begin
        Send CreateComObject of hoTickerObj
    End
    Get pvComObject of hoTickerObj to vTickerObj
    Get ComAddObjectAt2 Of hoListA 6 vTickerObj To iSuccess

    Get ComAppendString Of hoTickerObj "ticker" "GOOG" To iSuccess

    // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
    Get ComDeleteAt Of hoListA 7 To iSuccess
    Get Create (RefClass(cComChilkatJsonArray)) To hoAa
    If (Not(IsComObjectCreated(hoAa))) Begin
        Send CreateComObject of hoAa
    End
    Get pvComObject of hoAa to vAa
    Get ComAddArrayAt2 Of hoListA 7 vAa To iSuccess

    Get ComAddStringAt Of hoAa -1 "apple" To iSuccess
    Get ComAddIntAt Of hoAa -1 22 To iSuccess
    Get ComAddBoolAt Of hoAa -1 True To iSuccess
    Get ComAddNullAt Of hoAa -1 To iSuccess
    Get ComAddNumberAt Of hoAa -1 "1080.25" To iSuccess

    // Get the "fruit" array
    Get Create (RefClass(cComChilkatJsonArray)) To hoAFruit
    If (Not(IsComObjectCreated(hoAFruit))) Begin
        Send CreateComObject of hoAFruit
    End
    Get pvComObject of hoAFruit to vAFruit
    Get ComArrayAt2 Of hoJson 0 vAFruit To iSuccess

    // Get the 1st element:
    Get Create (RefClass(cComChilkatJsonObject)) To hoAppleObj
    If (Not(IsComObjectCreated(hoAppleObj))) Begin
        Send CreateComObject of hoAppleObj
    End
    Get pvComObject of hoAppleObj to vAppleObj
    Get ComObjectAt2 Of hoAFruit 0 vAppleObj To iSuccess

    // Modify values by member name:
    Get ComSetStringOf Of hoAppleObj "fruit" "fuji_apple" To iSuccess
    Get ComSetIntOf Of hoAppleObj "count" 46 To iSuccess
    Get ComSetBoolOf Of hoAppleObj "fresh" False To iSuccess
    Get ComSetStringOf Of hoAppleObj "extraInfo" "developed by growers at the Tohoku Research Station in Fujisaki" To iSuccess

    // Modify values by index:

    Get Create (RefClass(cComChilkatJsonObject)) To hoPearObj
    If (Not(IsComObjectCreated(hoPearObj))) Begin
        Send CreateComObject of hoPearObj
    End
    Get pvComObject of hoPearObj to vPearObj
    Get ComObjectAt2 Of hoAFruit 1 vPearObj To iSuccess

    Get ComSetStringAt Of hoPearObj 0 "bartlett_pear" To iSuccess
    Get ComSetIntAt Of hoPearObj 1 12 To iSuccess
    Get ComSetBoolAt Of hoPearObj 2 False To iSuccess
    Get ComSetStringAt Of hoPearObj 3 "harvested in late August to early September" To iSuccess

    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1


End_Procedure