Subject Re: Array
From Mervyn Bick <invalid@invalid.invalid>
Date Mon, 8 Mar 2021 16:34:58 +0200
Newsgroups dbase.getting-started
Attachment(s) parse_data.prg

On 2021/03/08 09:24, Rob Rickard wrote:
> Hi All
> can somebody please advise which is the best solution to use.
> Append from array OR extract out of a txt file.
> i need to get information out of this array
>
> [{"Id":2773,"Date":"2021-03-08T00:00:00+11:00","StartTime":1615147200,"EndTime":1615183200,"Mealbreak":"2021-03-08T00:30:00+11:00","Slots":[{"blnEmptySlot":false,"strType":"B","intStart":0,"intEnd":1800,"intUnixStart":1615147200,"intUnixEnd":1615149000,"mixedActivity":
....
EBA","Warning":"","WarningOverrideComment":"","Published":false,"MatchedByTimesheet":0,"Open":false,"ApprovalRequired":false,"ConfirmStatus":0,"ConfirmComment":"","ConfirmBy":0,"ConfirmTime":0,"SwapStatus":0,"SwapManageBy":null,"ShiftTemplate":1,"ConnectStatus":null,"Creator":1,"Created":"2021-03-04T10:30:27+11:00","Modified":"2021-03-04T10:30:57+11:00","OnCost":0,"StartTimeLocalized":"2021-03-08T07:00:00+11:00","EndTimeLocalized":"2021-03-08T17:00:00+11:00","ExternalId":null,"ConnectCreator":null,"BidsCount":null},{"Id":2775,"
>
> i have included the start of the 2nd shift - there are multiple shifts to deal with
> i do not need all the information only parts of it. eg Id, Date, starttime, finishtime, Employee
> i have never done anything with append from array so any help and suggestions would be greatly appreciated. i have saved the output to a txt file with no problems.so ....
>

This is not an array per se but rather a JSON object.  As it is not the
complete object it can't be used as a test case.

The attached program uses code from the help file but I've modified it
to save the parsed data to a text file rather than having it displayed
in the Result Panel.  With the data in a file you will be able to open
the file and browse the outcome at leisure instead of having the lines
flash by at speed.

You will need to write code to read the output file line by line. You
will need extract the data you want and save it to the appropriate field
in your table.

Once you've worked out how to deal with the parsed data the need to save
the data to a file will no longer be needed and you can deal with each
line as it becomes available.

If you post a result file and indicate which lines you are interested in
we can help with extracting the values and saving them to a table.

A big problem at the moment is that the start and end times appear to be
given in UNIX timestamp values.  I haven't found code in the dUFLP to
help but possibly someone "out there" has a solution.

Mervyn.










//Save the JSON object to be parsed to cString

f = new file()
f.open('whatever.txt')
cString = f.gets(f.size('whatever.txt'))
f.close()

// Added code to the example in the dBASE to save data to a text file

  cOutfile =  'parsed_data.txt'
   fOut =new file()
   fOut.create(cOutfile)
  

   j = new json()
   j.parse(cString)
//Check for Errors in string
   if j.hasParseError() //- returns True if last call to Parse() method encountered an error
      ?"error code: "+j.getParseErrorCode()//- returns numeric error code
      ?"error offset: "+j.getErrorOffset() //- returns numeric offset into JSON string where error was encountered
      ?"parse error: "+j.getParseError() //- returns string describing error (only available in English)
   else
      jsonListContent(j)
   endif
   fOut.close()


function jsonListContent
   parameters j
   private x, xtype
   static depth = -1, indent = 3
   jtype = type("j")
   depth++
   if jtype == 'O'
      // dBASE Object    
      if j.classname $ 'JSON|JSONMEMBER'
         if j.isObject()
            if j.membercount() > 0
               j.firstmember()
               for x=1 to j.membercount()                  
                  if j.member.isObject() or j.member.isArray()
                     // Recursively call this function for subobject or array
                     // Value will print as 'Object' or 'Array'
                     fOut.puts(space(depth*indent)+"*** "+j.member.name+" ***")
                     jsonListContent(j.member)
                  else
                     if depth < 2
                        fOut.puts( space(depth*indent)+j.member.name+iif(empty(j.member.value),' ','   '+j.member.value) )
                     endif  
                  endif
                  j.nextmember()
               endfor
            endif
         elseif j.isArray()
            if j.size() > 0
               for x = 1 to j.size()
                  etype = j.getat(x)
                  if etype == 'Object'
                     jsonListContent(j.getat(x))
                  else
                     if depth < 2
                        fOut.puts(space(depth*indent)+j.getat(x))
                     endif  
                  endif
               endfor
            endif
         endif
      endif
   endif
   depth--
   return