Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

QM and JPARSE

118 views
Skip to first unread message

Steve Trimble

unread,
Feb 8, 2024, 6:15:40 PM2/8/24
to OpenQM
Anybody using the JPARSE in QM?
I am working on an interface with 'shipengine' for retrieving ship rates and creating ship labels. The API responds in json. A simple request returns 1200 lines of text, double quotes, colons, commas, and eventually some 'data'.
Any one have some basic code they would care to share to help me understand 'data collections', JPARSE, etc?
All advise is appreciated.
thanks,
Steven T

Brandon Robinson

unread,
Feb 8, 2024, 6:19:48 PM2/8/24
to ope...@googlegroups.com
We have used the JSON.QUERY functions from this library before with good results. 



___________________

Brandon Robinson

Head Geek




--
You received this message because you are subscribed to the Google Groups "OpenQM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openqm+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openqm/67f924b8-b53a-45fa-ae8a-077dfc998b44n%40googlegroups.com.

Brian Speirs

unread,
Feb 8, 2024, 9:50:24 PM2/8/24
to OpenQM
Hi Steve,

The best thing to do is:

  * Get one of the API responses and save it into a directory file
  * Write a program to read the item, then
    - JPARSE the result
    - SCAN the output of JPARSE

Look in the documentation for details of SCAN.

A bit like Brandon, we need JSON before we had OpenQM - so we wrote our BUILD/PARSE utilities.

Cheers,

Brian

MAV

unread,
Feb 9, 2024, 2:13:39 AM2/9/24
to OpenQM
Hi, Steve

I use JPARSE and JBUILD frequently. For example, I use JPARSE to parse responses to calls to the WooCommerce REST API. The JSON documents generated by WooCommerce can be very large, especially when you want to obtain a list of products. 

I haven't had any problems.

Marcos Alonso Vega
INGESCO Sistemas Informáticos

KOSDAY SOLUTIONS

unread,
Feb 9, 2024, 2:45:15 AM2/9/24
to OpenQM
Same MAV, 
We use the QM collections a lot, and we are very satisfied. Below is a small example of parse, but in the QM manual everything is very well defined.

     COLEC.SER.BUFFER=COLLECTION()
     COLEC.SER.BUFFER=JPARSE(INPUT.JSON)
     **
     ID.REDSYS=COLEC.SER.BUFFER{'Ds_Order'}
     IDENTIFIER=COLEC.SER.BUFFER{'Ds_Merchant_Identifier'}
     EXPIRYDATE=COLEC.SER.BUFFER{'Ds_ExpiryDate'}    

Angel Rivas 

Brian Speirs

unread,
Feb 11, 2024, 6:49:37 PM2/11/24
to OpenQM
Hi Steve,

I'm not sure how you are getting on with this, but I tried the JPARSE function out of interest, and came up with the following code that works on most of the JSON samples I have on hand:

PROGRAM JS.TEST2
$INCLUDE Q.INCLUDES QB.COMMON.H

CRT CLR.SCR:'JSON Testing'
CRT

CALL Q.DIR.LIST('S', 'C:\Temp', 'F':@AM:'*.json', dirlist, emsg)
IF (emsg NE '') THEN
  CRT emsg
  STOP
END

args = 'JSON files':@AM:'Select file:'
filelist = dirlist<1>
afile = filelist<1, 1>
CALL Q.LISTBOX(dirlist<1>, args, afile, '', '')
IF (afile EQ ESC) THEN STOP
IF (afile EQ '') THEN STOP

fn = 'C:\Temp\':afile
OSREAD json FROM fn ELSE
  CRT 'Cannot read file: ':fn
  STOP
END

xpos = 41
oktypes = '1,2,3,5'

js = JPARSE(json)
names = ENUMERATE(js)
dcn = DCOUNT(names, @AM)
FOR nno = 1 TO dcn
  path = names<nno>
  node = js{path}
  GOSUB node.parse(path, node)
NEXT nno

STOP


LOCAL SUBROUTINE node.parse((path), (node))
  PRIVATE vt

  vt = VARTYPE(node)
  BEGIN CASE
    CASE (LISTINDEX(oktypes, ',', vt))
      CRT path:@(xpos):node
    CASE (vt EQ VT$NULLVALUE)
      CRT path:@(xpos):'null'
    CASE (vt EQ VT$COLLECTION)
      GOSUB node.parse.collection(path, node)
    CASE (vt EQ VT$ARRAY)
      GOSUB node.parse.array(path, node)
    CASE (1)
      CRT path:@(xpos):'Type: ':vt
  END CASE

  RETURN
END

LOCAL SUBROUTINE node.parse.array((path), (node))
  PRIVATE apath, elcnt, elem, elno, eltype

  elcnt = INMAT(node)
  FOR elno = 1 TO elcnt
    elem = node{elno}
    apath = path:'[':elno:']'
    eltype = VARTYPE(elem)
    BEGIN CASE
    CASE (LISTINDEX(oktypes, ',', eltype))
        CRT apath:@(xpos):elem
      CASE (eltype EQ VT$NULLVALUE)
        CRT apath:@(xpos):'null'
      CASE (eltype EQ VT$COLLECTION)
        GOSUB node.parse.collection(apath, elem)
      CASE (eltype EQ VT$ARRAY)
        GOSUB node.parse.array(apath, elem)
      CASE (1)
        CRT path:@(xpos):'Type: ':eltype
    END CASE
  NEXT elno

  RETURN
END

LOCAL SUBROUTINE node.parse.collection((path), (node))
  PRIVATE cnames, cname, cno, cnode, cpath, ctype, dcc

  cnames = ENUMERATE(node)
  dcc = DCOUNT(cnames, @AM)
  FOR cno = 1 TO dcc
    cname = cnames<cno>
    cpath = path:'/':cname
    cnode = node{cname}
    ctype = VARTYPE(cnode)
    BEGIN CASE
      CASE (LISTINDEX(oktypes, ',', ctype))
        CRT cpath:@(xpos):cnode
      CASE (ctype EQ VT$NULLVALUE)
        CRT cpath:@(xpos):'null'
      CASE (ctype EQ VT$COLLECTION)
        GOSUB node.parse.collection(cpath, cnode)
      CASE (ctype EQ VT$ARRAY)
        GOSUB node.parse.array(cpath, cnode)
      CASE (1)
        CRT path:@(xpos):'Type: ':ctype
    END CASE
  NEXT cno
 
  RETURN
END

END

You can largely ignore the first 25 lines. That is just getting the list of json files, and getting the user to select one of them. Just make sure you $INCLUDE the SYSCOM KEYS.H file.

You will find (if you haven't found out already), that the collection gets returned in alphabetic order - which is NOT the order of the data contained in the json item.

Hope this helps,

Brian
On Friday 9 February 2024 at 12:15:40 UTC+13 Steve Trimble wrote:

Steven Martin Trimble

unread,
Feb 12, 2024, 10:06:04 AM2/12/24
to ope...@googlegroups.com
Brian - this is absolutely most helpful!
Yes - I did find that ENUMERATE does bring 'certain' names alphabetically.
Your timing is perfect. I am back on this project this a.m.
I'll update later.
thanks again sir,

CDMI
Steven Trimble
(501) 772-3450 cell/text


--
You received this message because you are subscribed to the Google Groups "OpenQM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openqm+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages