Welcome to the SRP Forum! Please refer to the SRP Forum FAQ post if you have any questions regarding how the forum works.

srp_jsonx_go & go_back

This is the first time I've attempted to use these two commands and I don't know whether I'm misunderstanding something or I've just done it wrong, which I guess is the same thing. But it's not doing what I'm expecting at least.
I have a json array with six objects.
I loop through the array using go and back the way I think they're intended to be used, but I only retrieve the data of the odd numbered rows.
iteration 1 worked just fine and the data I wanted was retrieved.
Then I went go back and went through iteration 2 and everything returned null so I thought, Ok, I've got it wrong.
But then iteration 3 went through and all the correct data was returned.
Iteration 4 - nulls
Iteration 5 - data
Iteration 6 - nulls.
For itemCnt = 1 to numItems SRP_JsonX_Go('[':itemCnt:']') orderId = SRP_JsonX_Get('id') orderDate = SRP_JsonX_Get('date_created') custID = SRP_JsonX_Get('customer_id') custName = SRP_JsonX_Get('billing.first_name'):" ":SRP_JsonX_Get('billing.last_name') orderNo = SRP_JsonX_Get('number') orderStatus = SRP_JsonX_Get('status') custNote = SRP_JsonX_Get('customer_note') total = SRP_JsonX_Get('total') numLineItems = SRP_JsonX_Count('line_items') ; // for liCount = 1 to numLineItems SRP_JsonX_Go('line_items[':liCount:']') itemWebId = SRP_JsonX_Get('product_id') thisItem = SRP_JsonX_Get('sku') itemQty = SRP_JsonX_Get('quantity') itemPrice = SRP_JsonX_Get('price') itemTotal = SRP_JsonX_Get('total') SRP_JsonX_GoBack() ; // go back to the parent item (the line_items object) next liCount SRP_JsonX_GoBack() ; // go back to the parent item (the array object) Next itemCnt
The code is so much nicer to read then the long way round but I'm missing something. Well, 3 things - rows 2,4 and 6.

In typing this, I tested the code without the inner line_items loop. Without that loop, I get all six rows so the inner goback must be screwing me over somehow. But only every second time.

Comments

  • Mark,

    I've fallen into this trap before. The problem is with your inner "Go" command:
    SRP_JsonX_Go('line_items[':liCount:']')
    This works, but you are technically jumping forward two levels from the current element. The "GoBack" command doesn't revert to where you started, it "changes the current element to the parent of the current element." The solution is to add a second "GoBack".
  • So my first GoBack takes me back to 'line_items' and then I'm saying Go to 'line_items.line_items[':liCount:']' but because that doesn't work, the GoBack on the 2nd iteration takes me to where I thought I was?
  • Something like that...:) Did you test with the additional "GoBack" yet?
  • Just did and I was about to tell you it didn't work....

    But someone changed the source data on me and there was only 3 items to extract this time. I was debugging and thinking, well at least I didn't get any null rows. :)

    Looks like it works, thanks.
    Each of the inner loops only had one item so fingers crossed it still works if there are multiple iterations for that loop
  • I admit that I severely misnamed this routine. The docs explain it, but GoBack means "Go up to parent node". It does not go back to your previous position. Maybe I should create an alias called GoUp.
  • A "GoUp" alias would be useful. I would say, however, that part of the problem (for me at least) was not recognizing that performing a "Go" into a specific element of an array is jumping two levels deep rather than one level. That's why I intuitively expected the "GoBack" to work.
  • I think it's a valid intuition. If Go can "go anywhere" like when I use the web, then GoBack should be equivalent to Back on the browser. I could update GoBack to do this, but it would break stuff.
  • Funnily enough, I was just using JsonX this week and ran into this same issue and was mad at myself for choosing such a bad name.
Sign In or Register to comment.