HELP - parsing JSON

Just starting out? Need help? Post your questions and find answers here.
l1marik
User
User
Posts: 52
Joined: Tue Jun 30, 2020 6:22 am

HELP - parsing JSON

Post by l1marik »

I am newbie in JSON and I have follow JSON:

Code: Select all

{"files":[["EAC_D_20221227.eac",91,1672156042,1672156164],["EAC_L_00000011.eac",102720,1672156148,1672221568],["EAC_D_20221228.eac",97254,1672222672,1672319576],["EAC_D_20221229.eac",135280,1672319622,1672416862],["EAC_D_20221230.eac",70225,1672416948,1672476938],["EAC_D_20221231.eac",153197,1672477034,1672577988],["EAC_D_202311.eac",40275,1672581830,1672611446],["EAC_D_202312.eac",536856,1672676112,1673023516],["EAC_D_202316.eac",457,1673023554,1673023856],["EAC_D_20230106.eac",112278,1673024056,1673099980],["EAC_D_20230107.eac",103538,1673100078,1673168604],["EAC_D_20230108.eac",581664,1673170824,1673543754],["EAC_D_20230112.eac",126746,1673543838,1673626162],["EAC_D_20230113.eac",91,1673626340,1673626402]]}
how to parse it to follow structure:

Code: Select all

Structure list_file
  name.s
  size.i
  creation_time.i
  last_write.i
EndStructure

Structure sd_files
  Array files.list_file(1)
EndStructure
or array:

Code: Select all

Dim files.list_file(1)
THX
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: HELP - parsing JSON

Post by boddhi »

Hello,

With a list :

Code: Select all

Structure list_file
  name.s
  size.i
  creation_time.i
  last_write.i
EndStructure

NewList sd_files.list_file()

Files.s ="["+
        "{'name':'EAC_D_20221227.eac' ,'size':91,      'creation_time':1672156042  ,'last_write':1672156164},"+
        "{'name':'EAC_L_00000011.eac' ,'size':102720,  'creation_time':1672156148  ,'last_write':1672221568},"+
        "{'name':'EAC_D_20221228.eac' ,'size':97254,   'creation_time':1672222672  ,'last_write':1672319576},"+
        "{'name':'EAC_D_20221229.eac' ,'size':135280,  'creation_time':1672319622  ,'last_write':1672416862},"+
        "{'name':'EAC_D_20221230.eac' ,'size':70225,   'creation_time':1672416948  ,'last_write':1672476938},"+
        "{'name':'EAC_D_20221231.eac' ,'size':153197,  'creation_time':1672477034  ,'last_write':1672577988},"+
        "{'name':'EAC_D_202311.eac'   ,'size':40275,   'creation_time':1672581830  ,'last_write':1672611446},"+
        "{'name':'EAC_D_202312.eac'   ,'size':536856,  'creation_time':1672676112  ,'last_write':1673023516},"+
        "{'name':'EAC_D_202316.eac'   ,'size':457,     'creation_time':1673023554  ,'last_write':1673023856},"+
        "{'name':'EAC_D_20230106.eac' ,'size':112278,  'creation_time':1673024056  ,'last_write':1673099980},"+
        "{'name':'EAC_D_20230107.eac' ,'size':103538,  'creation_time':1673100078  ,'last_write':1673168604},"+
        "{'name':'EAC_D_20230108.eac' ,'size':581664,  'creation_time':1673170824  ,'last_write':1673543754},"+
        "{'name':'EAC_D_20230112.eac' ,'size':126746,  'creation_time':1673543838  ,'last_write':1673626162},"+
        "{'name':'EAC_D_20230113.eac' ,'size':91,      'creation_time':1673626340  ,'last_write':1673626402}"+
        "]"

ReplaceString(Files,"'",Chr(34),#PB_String_InPlace)

If ParseJSON(0,Files)
  ExtractJSONList(JSONValue(0),sd_files())
  ForEach sd_files()
    With sd_files()
      Debug "Filename : "+\name
      Debug "  Size : "+\size
      Debug "  Creation time : "+\creation_time
      Debug "  Last write : "+\last_write
    EndWith
  Next
EndIf
With an array :

Code: Select all

Structure list_file
  name.s
  size.i
  creation_time.i
  last_write.i
EndStructure

Dim sd_files.list_file(13)

Files.s ="["+
        "{'name':'EAC_D_20221227.eac' ,'size':91,      'creation_time':1672156042  ,'last_write':1672156164},"+
        "{'name':'EAC_L_00000011.eac' ,'size':102720,  'creation_time':1672156148  ,'last_write':1672221568},"+
        "{'name':'EAC_D_20221228.eac' ,'size':97254,   'creation_time':1672222672  ,'last_write':1672319576},"+
        "{'name':'EAC_D_20221229.eac' ,'size':135280,  'creation_time':1672319622  ,'last_write':1672416862},"+
        "{'name':'EAC_D_20221230.eac' ,'size':70225,   'creation_time':1672416948  ,'last_write':1672476938},"+
        "{'name':'EAC_D_20221231.eac' ,'size':153197,  'creation_time':1672477034  ,'last_write':1672577988},"+
        "{'name':'EAC_D_202311.eac'   ,'size':40275,   'creation_time':1672581830  ,'last_write':1672611446},"+
        "{'name':'EAC_D_202312.eac'   ,'size':536856,  'creation_time':1672676112  ,'last_write':1673023516},"+
        "{'name':'EAC_D_202316.eac'   ,'size':457,     'creation_time':1673023554  ,'last_write':1673023856},"+
        "{'name':'EAC_D_20230106.eac' ,'size':112278,  'creation_time':1673024056  ,'last_write':1673099980},"+
        "{'name':'EAC_D_20230107.eac' ,'size':103538,  'creation_time':1673100078  ,'last_write':1673168604},"+
        "{'name':'EAC_D_20230108.eac' ,'size':581664,  'creation_time':1673170824  ,'last_write':1673543754},"+
        "{'name':'EAC_D_20230112.eac' ,'size':126746,  'creation_time':1673543838  ,'last_write':1673626162},"+
        "{'name':'EAC_D_20230113.eac' ,'size':91,      'creation_time':1673626340  ,'last_write':1673626402}"+
        "]"

ReplaceString(Files,"'",Chr(34),#PB_String_InPlace)

If ParseJSON(0,Files)
  ExtractJSONArray(JSONValue(0),sd_files())
  For count=0 To 13
    With sd_files(count)
      Debug "Filename : "+\name
      Debug "  Size : "+\size
      Debug "  Creation time : "+\creation_time
      Debug "  Last write : "+\last_write
    EndWith
  Next
EndIf
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
nsstudios
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: HELP - parsing JSON

Post by nsstudios »

If you need files to be nested, maybe because you want to have more fields in the future...

Code: Select all

EnableExplicit
Define Data$=~"{\"extraInfo\":\"test\",\"files\":[{\"name\":\"EAC_D_20221227.eac\",\"size\":91,\"creation_time\":1672156042,\"last_write\":1672156164},{\"name\":\"EAC_L_00000011.eac\",\"size\":102720,\"creation_time\":1672156148,\"last_write\":1672221568},{\"name\":\"EAC_D_20221228.eac\",\"size\":97254,\"creation_time\":1672222672,\"last_write\":1672319576},{\"name\":\"EAC_D_20221229.eac\",\"size\":135280,\"creation_time\":1672319622,\"last_write\":1672416862},{\"name\":\"EAC_D_20221230.eac\",\"size\":70225,\"creation_time\":1672416948,\"last_write\":1672476938},{\"name\":\"EAC_D_20221231.eac\",\"size\":153197,\"creation_time\":1672477034,\"last_write\":1672577988},{\"name\":\"EAC_D_202311.eac\",\"size\":40275,\"creation_time\":1672581830,\"last_write\":1672611446},{\"name\":\"EAC_D_202312.eac\",\"size\":536856,\"creation_time\":1672676112,\"last_write\":1673023516},{\"name\":\"EAC_D_202316.eac\",\"size\":457,\"creation_time\":1673023554,\"last_write\":1673023856},{\"name\":\"EAC_D_20230106.eac\",\"size\":112278,\"creation_time\":1673024056,\"last_write\":1673099980},{\"name\":\"EAC_D_20230107.eac\",\"size\":103538,\"creation_time\":1673100078,\"last_write\":1673168604},{\"name\":\"EAC_D_20230108.eac\",\"size\":581664,\"creation_time\":1673170824,\"last_write\":1673543754},{\"name\":\"EAC_D_20230112.eac\",\"size\":126746,\"creation_time\":1673543838,\"last_write\":1673626162},{\"name\":\"EAC_D_20230113.eac\",\"size\":91,\"creation_time\":1673626340,\"last_write\":1673626402}]}"
Structure list_file
  name.s
  size.i
  creation_time.i
  last_write.i
EndStructure

Structure fileStruct
extraInfo.s
Array files.list_file(0)
EndStructure
Dim arr.fileStruct(0)
If Not ParseJSON(0, Data$, #PB_JSON_NoCase)
DebuggerError(""+JSONErrorLine()+"/"+JSONErrorPosition()+" ("+Mid(Data$, JSONErrorPosition(), 1)+"): "+JSONErrorMessage())
EndIf
ExtractJSONStructure(JSONValue(0), @arr(), fileStruct)
FreeJSON(0)
Define i, j
For i=0 To ArraySize(arr())
Debug arr(i)\extraInfo
Debug "files:"
For j=0 To ArraySize(arr(i)\files())
With arr(i)\files(j)
Debug \name
Debug \size
Debug \creation_time
Debug \last_write
EndWith
Next j
Next i
Json (unfortunately) always needs the "key":value pairs.
In this case, I just converted ["str",x,y,z] by using regex:

Code: Select all

\[(\".+?\"),\s*(\d+?),\s*(\d+?),\s*(\d+)\]
{\"name\":\1,\"size\":\2,\"creation_time\":\3,\"last_write\":\4}
There's also Freak's json structure maker that could be useful in the future.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HELP - parsing JSON

Post by infratec »

The problem is:

Your JSON is valid, but not really compatible with PBs JSON utilities.
Then I find it easier to don't use the JSON library and do it 'by hand'.

Code: Select all

EnableExplicit

Structure FileList_Structure
  name.s
  size.i
  creation_time.i
  last_write.i
EndStructure


Procedure.i DecodeFileListJSON(JSON$, List FileList.FileList_Structure())
  
  Protected.i Pos1, Pos2, i
  Protected JSONArray$, Help$
  
  Pos1 = FindString(JSON$, "[[")
  If Pos1
    Pos2 = FindString(JSON$, "]]", Pos1)
    If Pos2
      JSON$ = Mid(JSON$, Pos1 + 1, Pos2 - Pos1)
      ;Debug JSON$
      
      i = 1
      Repeat
        JSONArray$ = StringField(JSON$, i, "],[")
        ;Debug JSONArray$
        If JSONArray$ <> ""
          Help$ = LTrim(JSONArray$, "[")
          ;Debug Help$
          
          AddElement(FileList())
          FileList()\name = StringField(Help$, 1, ",")
          FileList()\size = Val(StringField(Help$, 2, ","))
          FileList()\creation_time = Val(StringField(Help$, 3, ","))
          FileList()\last_write = Val(StringField(Help$, 4, ","))
        EndIf
        i + 1
      Until JSONArray$ = ""
      
    EndIf
  EndIf
  
  ProcedureReturn ListSize(FileList())
  
EndProcedure




Define JSON$
NewList FileList.FileList_Structure()

JSON$ = ~"{\"files\":[[\"EAC_D_20221227.eac\",91,1672156042,1672156164],[\"EAC_L_00000011.eac\",102720,1672156148,1672221568],[\"EAC_D_20221228.eac\",97254,1672222672,1672319576],[\"EAC_D_20221229.eac\",135280,1672319622,1672416862],[\"EAC_D_20221230.eac\",70225,1672416948,1672476938],[\"EAC_D_20221231.eac\",153197,1672477034,1672577988],[\"EAC_D_202311.eac\",40275,1672581830,1672611446],[\"EAC_D_202312.eac\",536856,1672676112,1673023516],[\"EAC_D_202316.eac\",457,1673023554,1673023856],[\"EAC_D_20230106.eac\",112278,1673024056,1673099980],[\"EAC_D_20230107.eac\",103538,1673100078,1673168604],[\"EAC_D_20230108.eac\",581664,1673170824,1673543754],[\"EAC_D_20230112.eac\",126746,1673543838,1673626162],[\"EAC_D_20230113.eac\",91,1673626340,1673626402]]}"

If DecodeFileListJSON(JSON$, FileList())
  ForEach FileList()
    Debug FileList()\name + " " + Str(FileList()\size) + " " + FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss", FileList()\creation_time) + " " + FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss", FileList()\last_write)
  Next
EndIf
Post Reply