NetTalk Central

NetTalk Web Server => Web Server - Ask For Help => Topic started by: BradT on September 17, 2018, 11:24:20 AM

Title: JFiles, Receiving JSON
Post by: BradT on September 17, 2018, 11:24:20 AM
I have the following JSON coming in:

{
  "response": {
    "loop": [
      {
        "loop_id": "22719676",
        "main_type": "shipment",
        "main_id": "366211",
        "amount": {
          "currency": "usd",
          "amount": 9.99
        },
        "zone": null,
        "type": null,
        "days": 1
      },
     
      "loop" iterates here....
      ]
 )     
     
I used jsonreturn1.LoadNodes(returnQueue,'loop') and it loaded the queue with all the iterations of "loop", plus some extra.
I tried to use jsonreturn1.LoadNodes(chargesQueue,'amount',chargesQueue.main_id,'main_id') without much luck.  I'm a few days into this now so I have tried many other things.

How do I load such JSON as above, and I guess the bigger question I have it how does JFiles loop through the JSON so I can load any way I need to load?

Title: Re: JFiles, Receiving JSON
Post by: bshields on September 17, 2018, 04:41:08 PM
Hi Brad,

Probably lots of ways, I do something like this.

json         JSONClass
jsonLoop      &JSONClass
st     StringTheory

LoopQ         QUEUE,PRE(_LQ)
Loop_ID         STRING(20),NAME('loop_id')
Main_Type       STRING(20),NAME('main_type')
...
              .


CODE
      st.SetValue('SOMEJSONINHERE')
      json.start()
      json.tagCase             = jF:CaseAsIs
      json.RemovePrefix        = True
      json.LoadString(st)
      jsonLoop &= json.GetByName('loop')
      IF NOT jsonLoop &= NULL
        IF jsonLoop.Load(LoopQ) = jf:Ok
          !Do somethign with queue
        .
      .

Regards
Bill

Title: Re: JFiles, Receiving JSON
Post by: BradT on September 17, 2018, 05:27:50 PM
Thanks

How does it drill down into the amount as below?  That iteration can come many times inside the "Loop".  Ive done this many times with XFiles but not that familiar with JFiles yet.  :)

"amount": {
          "currency": "usd",
          "amount": 9.99
        },
Title: Re: JFiles, Receiving JSON
Post by: bshields on September 17, 2018, 08:20:48 PM
Hi Brad,

When I have to do this I loop through the json nodes using:

jsonItem  &= jsonLoop.Get(i#)

Then locate the parent node:

jsonObject &= jsonItem.GetByName('amount')

then load the node into a group:

e# = jsonObject.Load(AmountG)

then merge then data into my main record

LoopQ :=: AmountG


However, i've always suspected there is a better way, so hopefully someone else will chime in with a more effective method.

Regards
Bill




Title: Re: JFiles, Receiving JSON
Post by: Bruce on September 17, 2018, 09:24:14 PM
Hi Brad,

>> How do I load such JSON as above,

It's a simple queue, so I'm not sure what you are struggling with.
Perhaps the best solution for you is to go back to the simple queue import and then, if you have issues there post a small example of your json file, and your code.

If you have problems getting this to work - that's likely just a case of inexperience with the tool - and trying more complicated methods are not going to help if you haven't got the fundamentals right.

cheers
Bruce
Title: Re: JFiles, Receiving JSON
Post by: BradT on September 18, 2018, 07:55:38 AM
It's actually a queue within a queue.   

Thanks Guys.
Title: Re: JFiles, Receiving JSON
Post by: BradT on September 18, 2018, 09:48:44 AM
I just added a group within my receiving queue and it  Load worked fine.  As always, I overcomplicate Bruce's solutions.

Bruce,  is there a set of embeds that allow us to see the iterations during a Load?  Maybe to manipulate the data as its being loaded into the queue?

I assume AddQueueRecord?
Title: Re: JFiles, Receiving JSON
Post by: Bruce on September 18, 2018, 10:44:40 PM
there are a bunch of things you can do during the load.

ValidateRecord lets you suppress a record, (http://www.capesoft.com/docs/jfiles/jfiles.htm#FilteringOnLoad)
AddQueueRecord lets you prime other fields, (http://www.capesoft.com/docs/jfiles/jfiles.htm#AddQueueRecord)
DeformatValue lets you change the json value into a Clarion value (like for dates and times) http://www.capesoft.com/docs/jfiles/jfiles.htm#DeformattingJSON

and so on.

So I guess give the docs a read and you'll see all kinds of things....

Cheers
Bruce