Lianja
Lianja
Find and Delete Object from JSON Array
See more JSON Examples
Demonstrates how to find an delete an object from a JSON array of objects.Chilkat Lianja Downloads
llSuccess = .F.
// This example will delete the "Pasta" category object from Menu B.
// {
// "menus": [
// {
// "name": "Menu A",
// "categories": [
// {
// "name": "Kebabs",
// "description": "blah blah blah"
// },
// {
// "name": "Burgers",
// "description": "blah blah blah"
// },
// {
// "name": "Pasta",
// "description": "blah blah blah"
// },
// {
// "name": "Appetizers",
// "description": "blah blah blah"
// }
// ]
// },
// {
// "name": "Menu B",
// "categories": [
// {
// "name": "Kebabs",
// "description": "blah blah blah"
// },
// {
// "name": "Burgers",
// "description": "blah blah blah"
// },
// {
// "name": "Pasta",
// "description": "blah blah blah"
// },
// {
// "name": "Appetizers",
// "description": "blah blah blah"
// }
// ]
// }
// ]
// }
loJson = createobject("CkJsonObject")
llSuccess = loJson.LoadFile("qa_data/json/menus.json")
if (llSuccess = .F.) then
? loJson.LastErrorText
release loJson
return
endif
// Find Menu B.
loMenu = loJson.FindRecord("menus","name","Menu B",.T.)
// assume menu is found..
// Get the categories array, and then find the index of the "Pasta" menu category.
loCatArr = loMenu.ArrayOf("categories")
// Assume it is found..
// Find the object where the name of the JSON member is "name", and the value is "Pasta"
lnIndex = loCatArr.FindObject("name","Pasta",.T.)
if (lnIndex < 0) then
? "Pasta not found."
else
// Delete the "Pasta" category record from Menu B
llSuccess = loCatArr.DeleteAt(lnIndex)
// Assume OK..
endif
// Examine the JSON to see if it worked as expected..
loJson.EmitCompact = .F.
? loJson.Emit()
release loCatArr
release loMenu
release loJson