I am also working with json. I am getting json from webhook in this test case from localhost.
The test json is:
Code: Select all
{"first":[{"name":"John", "age":30, "car":"Mazda"},
{"name":"Tom", "age":31, "car":"Suzuki"},
{"name":"Peter", "age":32, "car":"honda"}],
"second": [{"name":"Donald", "age":40, "car":"honda"},
{"name":"Hans", "age":41, "car":"honda"},
{"name":"Robin", "age":42, "car":"toyota"}]
}
So I did have hard time to find how to handle json with objects and arrays so I decided to try DeepSeek 3.2 thinking model. After testing I am in impression that It seems to understand Spiderbasic (and Purebasic) actually very well and gives proper working solutions. Sometimes it is good to note DeebSeek that we are actually using Spiderbasic not Purebasic.
Here is three options to handle the json above.
Completely dynamic approach (most flexible):
Code: Select all
Structure JSONField
Key.s
Value.s
Type.s ; "string", "number", "boolean", "null", "object", "array"
EndStructure
Procedure.s GetJSONValueAsString(JSONValue)
; Try to extract value as string regardless of type
If JSONValue = 0
ProcedureReturn "[null]"
EndIf
; Try string
Result.s = GetJSONString(JSONValue)
If Result <> ""
ProcedureReturn Result
EndIf
; Try integer
IntValue = GetJSONInteger(JSONValue)
ProcedureReturn Str(IntValue) ; Will return "0" for false/0/null, but it's a limitation
; Note: We can't easily distinguish between types without type checking functions
EndProcedure
Procedure DisplayJSONObject(JSONItem, Indent.s = " ")
; This is a recursive function to display any JSON structure
If JSONItem = 0
Debug Indent + "[null or invalid]"
ProcedureReturn
EndIf
; Try to see if it's an object with members
; We'll check for common properties as a heuristic
NameMember = GetJSONMember(JSONItem, "name")
If NameMember
; Likely a person object
Debug Indent + "Object:"
; List of common property names to check
Dim Properties.s(5)
Properties(0) = "name"
Properties(1) = "age"
Properties(2) = "car"
Properties(3) = "email"
Properties(4) = "phone"
Properties(5) = "city"
For p = 0 To ArraySize(Properties()) - 1
Member = GetJSONMember(JSONItem, Properties(p))
If Member
Debug Indent + " " + Properties(p) + ": " + GetJSONValueAsString(Member)
EndIf
Next
; Could add more property checks here
Else
; Unknown object type - try to display as value
Debug Indent + "Value: " + GetJSONValueAsString(JSONItem)
EndIf
EndProcedure
Procedure HttpGetEvent(Success, Result$, UserData)
If Success
Debug "=== Completely Dynamic JSON Processing ==="
ParseJSON(0, Result$)
Root = JSONValue(0)
; First, display the entire structure
Debug ""
Debug "Full structure:"
DisplayJSONObject(Root, "")
; Now process known arrays
Debug ""
Debug "=== Processing Arrays ==="
; Arrays we expect
Arrays$ = "first,second"
For a = 1 To CountString(Arrays$, ",") + 1
ArrayName.s = StringField(Arrays$, a, ",")
Debug ""
Debug "Array: " + ArrayName
JSONArray = GetJSONMember(Root, ArrayName)
If JSONArray
ArraySize = JSONArraySize(JSONArray)
Debug " Size: " + Str(ArraySize)
For i = 0 To ArraySize - 1
Item = GetJSONElement(JSONArray, i)
Debug " Item " + Str(i+1) + ":"
DisplayJSONObject(Item, " ")
Next
EndIf
Next
Else
Debug "HTTPRequest(): Error"
EndIf
EndProcedure
HTTPRequest(#PB_HTTP_Get, "http://127.0.0.1:88/json.php", "", @HttpGetEvent())
Hybrid approach with fallback (recommended)
Code: Select all
Procedure HttpGetEvent(Success, Result$, UserData)
If Success
Debug "=== Hybrid JSON Processing ==="
ParseJSON(0, Result$)
Root = JSONValue(0)
; Process each group
Arrays$ = "first,second"
For a = 1 To CountString(Arrays$, ",") + 1
ArrayName.s = StringField(Arrays$, a, ",")
Debug ""
Debug "=== Processing '" + ArrayName + "' ==="
JSONArray = GetJSONMember(Root, ArrayName)
If JSONArray
ArraySize = JSONArraySize(JSONArray)
For i = 0 To ArraySize - 1
Item = GetJSONElement(JSONArray, i)
If Item
Debug "Item " + Str(i+1) + ":"
; List of property names to try
; You can expand this list based on your needs
PropertyNames$ = "name,age,car,email,phone,city,country,active,enabled,id,username"
For p = 1 To CountString(PropertyNames$, ",") + 1
PropName.s = StringField(PropertyNames$, p, ",")
Member = GetJSONMember(Item, PropName)
If Member
; Get value as string (works for strings and numbers)
Value.s = GetJSONString(Member)
If Value = ""
; Might be number 0 or boolean false
NumValue = GetJSONInteger(Member)
Value = Str(NumValue)
EndIf
If Value <> "" And Value <> "0" ; Could be actual 0
Debug " " + PropName + ": " + Value
ElseIf Value = "0"
; Check if it's actually 0 or just missing
Debug " " + PropName + ": 0"
EndIf
EndIf
Next
EndIf
Next
EndIf
Next
Else
Debug "HTTPRequest(): Error"
EndIf
EndProcedure
HTTPRequest(#PB_HTTP_Get, "http://127.0.0.1:88/json.php", "", @HttpGetEvent())
This example is hard coded just for above json structure.
Code: Select all
Procedure HttpGetEvent(Success, Result$, UserData)
If Success
Debug "JSON Response:"
Debug Result$
Debug ""
Structure Person
name.s
age.l
car.s
EndStructure
ParseJSON(0, Result$)
Root = JSONValue(0)
; Access first array
Debug "=== First Group ==="
First = GetJSONMember(Root, "first")
If First
FirstSize = JSONArraySize(First)
For i = 0 To FirstSize - 1
Define Person.Person
ExtractJSONStructure(GetJSONElement(First, i), @Person, Person)
Debug Person\name + " | Age: " + Str(Person\age) + " | Car: " + Person\car
Next
EndIf
Debug ""
Debug "=== Second Group ==="
Second = GetJSONMember(Root, "second")
If Second
SecondSize = JSONArraySize(Second)
For i = 0 To SecondSize - 1
Define Person.Person
ExtractJSONStructure(GetJSONElement(Second, i), @Person, Person)
Debug Person\name + " | Age: " + Str(Person\age) + " | Car: " + Person\car
Next
EndIf
Else
Debug "HTTPRequest(): Error"
EndIf
EndProcedure
HTTPRequest(#PB_HTTP_Get, "http://127.0.0.1:88/json.php", "", @HttpGetEvent())