Read JSON File
Posted: Thu Feb 08, 2018 3:31 am
Struggling with JSON, never used the PB JSON lib before. I am going to work with large-ish JSON files produced by others. No two files are the same, so the structure or order of the data is an unknown. They each define a window/form. Components (Gadgets) on the form can be in containers, which can themselves be in containers, but the sample file linked does not have that complexity.
Sample.txt
What I need to do is read the file, extract all the Key-Value pairs, modify some of them, write a new file. I'm stuck on the extraction bit!
Using the code examples in the PB Help, if I cut-out a portion of the file between and including {} brackets (an Object), I can produce a list of Key-Value pairs from that snippet. If however I try to process the whole file string, only JSON Objects are found and if I try to extract Key-Value pairs from the Objects, my code fails:
[/size]
It seems to me that there should be a better way to approach this task anyway. I'm wondering though if the JSON format of the file fully complies with the standard that the PB Lib expects.......
Sample.txt
What I need to do is read the file, extract all the Key-Value pairs, modify some of them, write a new file. I'm stuck on the extraction bit!
Using the code examples in the PB Help, if I cut-out a portion of the file between and including {} brackets (an Object), I can produce a list of Key-Value pairs from that snippet. If however I try to process the whole file string, only JSON Objects are found and if I try to extract Key-Value pairs from the Objects, my code fails:
Code: Select all
Enumeration
#FileIO
#Jason
EndEnumeration
Global sFile.s = "C:\Sample.txt"
Structure JSON_objects
sObjKey.s
iObjSize.i
EndStructure
Global igJval.i
Global NewList gJsonObjectsList.JSON_objects()
Global NewList gJsonSubObjectsList.JSON_objects()
Procedure.s PfReadFile()
;#----------------------
Protected sSkip.s, sJason.s
If ReadFile(#FileIO, sFile)
sSkip = ReadString(#FileIO, #PB_Ascii)
sSkip = ReadString(#FileIO, #PB_Ascii)
sJason = ReadString(#FileIO, #PB_Ascii)
CloseFile(#FileIO)
ProcedureReturn(sJason)
Else
MessageRequester("Fail","Could not open file")
ProcedureReturn "FAIL"
EndIf
EndProcedure
Procedure PfParseObjs()
;#----------------------
Protected iItem.i
Protected Dim sArray.s(0)
ForEach gJsonObjectsList()
For iItem = 0 To gJsonObjectsList()\iObjSize
ParseJSON(#Jason, gJsonObjectsList()\sObjKey)
igJval = JSONValue(#Jason)
If ExamineJSONMembers(igJval)
Select JSONType(igJval)
Case #PB_JSON_Null: Debug "Null"
Case #PB_JSON_String: Debug JSONMemberKey(igJval) + ": " + GetJSONString(GetJSONMember(igJval, JSONMemberKey(igJval)))
Case #PB_JSON_Number: Debug JSONMemberKey(igJval) + ": " + StrD(GetJSONDouble(GetJSONMember(igJval, JSONMemberKey(igJval))),4)
Case #PB_JSON_Boolean: Debug JSONMemberKey(igJval) + ": " + Str(GetJSONBoolean(GetJSONMember(igJval, JSONMemberKey(igJval))))
Case #PB_JSON_Array
Debug JSONMemberKey(igJval) + ": "
ExtractJSONArray(GetJSONMember(igJval, JSONMemberKey(igJval)), sArray())
For iItem = 0 To ArraySize(sArray())
Debug sArray(iItem)
Next
Case #PB_JSON_Object
AddElement(gJsonSubObjectsList())
gJsonSubObjectsList()\sObjKey = JSONMemberKey(igJval)
Debug gJsonSubObjectsList()\sObjKey
gJsonSubObjectsList()\iObjSize = JSONObjectSize(igJval)
EndSelect
EndIf
Next
Next
EndProcedure
Procedure PfJason()
;#-----------------
Protected iItem.i
Protected Dim sArray.s(0)
Protected sJason.s = PfReadFile()
If Not (sJason = "FAIL")
ParseJSON(#Jason, sJason)
igJval = JSONValue(#Jason)
If ExamineJSONMembers(igJval)
While NextJSONMember(igJval)
Select JSONType(igJval)
Case #PB_JSON_Null: Debug "Null"
Case #PB_JSON_String: Debug JSONMemberKey(igJval) + ": " + GetJSONString(GetJSONMember(igJval, JSONMemberKey(igJval)))
Case #PB_JSON_Number: Debug JSONMemberKey(igJval) + ": " + StrD(GetJSONDouble(GetJSONMember(igJval, JSONMemberKey(igJval))),4)
Case #PB_JSON_Boolean: Debug JSONMemberKey(igJval) + ": " + Str(GetJSONBoolean(GetJSONMember(igJval, JSONMemberKey(igJval))))
Case #PB_JSON_Array
Debug JSONMemberKey(igJval) + ": "
ExtractJSONArray(GetJSONMember(igJval, JSONMemberKey(igJval)), sArray())
For iItem = 0 To ArraySize(sArray())
Debug sArray(iItem)
Next
Case #PB_JSON_Object
AddElement(gJsonObjectsList())
gJsonObjectsList()\sObjKey = JSONMemberKey(igJval)
Debug gJsonObjectsList()\sObjKey
gJsonObjectsList()\iObjSize = JSONObjectSize(igJval)
EndSelect
Wend
EndIf
EndIf
Debug "==============================="
If(ListSize(gJsonObjectsList()) > 0) : PfParseObjs() : EndIf
EndProcedure
PfJason()
End
It seems to me that there should be a better way to approach this task anyway. I'm wondering though if the JSON format of the file fully complies with the standard that the PB Lib expects.......