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

SRP_JSON guidance

Hello all (and Happy New Year)

I obviously partook a tad too much over the festive season as now I am having a brain fade.

I have a comma-seperated array of values that I am trying to set using SRP_JSON (SETVALUE). Techincally it is a string but I need to pass it to the 3rd Party as not one ...

I used this:
SRP_JSON(ROOTHANDLE, "SETVALUE", "JobId", "[":ListUnDelivered:"]")

I need to have this:
{"JobId":[6130,6138,6149,6170,6134,6152,6155,6143,6161,6164,6137,6158,6167,6140]}

I actually get:
{"JobId":"[6130,6138,6149,6170,6134,6152,6155,6143,6161,6164,6137,6158,6167,6140]"}

Unfortunately the 3rd party then treats the above as a single job and hence fails on multiple levels.

Maybe I am using the wrong Service? How can I add to JSON without the quotes identifying it as a string?
I know I can workaround this a couple of ways but Id rather use SRP_JSON right ....

Comments

  • Your 3rd Party is expecting your comma delimited array to be formatted as a JSON array (which make sense). The SETVALUE service only sets simple (i.e., single-value) data values. Thus, you are asking SRP_JSON to treat your entire string to be a simple string.

    There are various ways of approaching this, but I think the most efficient way is to first create an array object using the NEW service:

    SRP_JSON(arrayUndelivered, 'New', 'Array', LIstUndelivered, ',')

    Then SET this as the value to your JobId property:

    SRP_JSON(ROOTHANDLE, 'Set', 'JobId', arrayUndelivered)

    Of course, RELEASE your array since you no longer need it:

    SRP_JSON(arrayUndelivered, 'Release')

    P.S. This is untested code and I am not near a copy of OI to even verify the syntax.
  • I'd suggest trying their newish option AddValueArray

    I haven't tried it yet myself but basically it would be something like this
    Result = SRP_Json(jobHandle, "New", "ARRAY")
    SRP_Json(jobHandle, "AddValueArray", ListUnDelivered, ",")
    SRP_JSON(ROOTHANDLE, "SET", "JobId", jobHandle)

    Untested but I think that's the gist
  • As I noted, there are multiple ways of doing this. Both @AusMarkB and my code rely on relatively new features introduced in v2.1.1. My code only saves one line of code by populating the array at the same time it is created.
  • yet had I seen your comment @DonBakke, before clicking Post Comment, I probably would have back pedaled and not bothered. You must type faster than me.
  • Thank you gentlemen. I quickly tried both. @DonBakke, your solution worked out of the box, @AusMarkB, your solution certainly got rid of the quotes but seemed to double up on the square brackets (i.e. double object). I didn't bother to investigate why at that stage since Don's had already proven successful so I just went with that.

    I think that just proves I need to read release notes a little more thoroughly (or more to the point, remember them after I read them!)

    Cheers!
  • @opto_will coincidentally, I was sent an email this morning to act as a second pair of eyes on that exact thing; double object as a result of addValueArray. Having not used it myself, I hadn't seen that but that's twice mentioned within a couple of hours.
    I tried a couple of tweaks but didn't find a way that it worked as expected or at least as I expected but maybe I've misunderstood its intent?
    Perhaps @kevinfournier can shed some light on it?

    Here's an example of what's being referred to

    Create an array using "NEW" but no initialisation.
    Use AddValueArray to populate it
    End up with an array within an array.

    "site_address": [
    [
    "Level 3",
    "21 Smith St"
    ]
    ],

    when what was expected was

    "site_address": [
    "Level 3",
    "21 Smith St"
    ],
  • edited January 8
    AddValueArray adds an OI delimited array. You are passing JSON. If you pass this:

    [ "Level 3", "21 Smith St" ]
    Then it is parsed (using the delimiter you passed in, which was a comma), then AddValueArray breaks it into two elements...

    [ "Level 3"
    ...and...

    "21 Smith St" ]
    ...and adds those two elements into a JSON array. That's why you get the double brackets. Again, this is for passing standard OI arrays like this:

    Array = "Level 3":@VM:"21 Smith St" SRP_Json(Handle, "AddValueArray", Array, @VM)
    Rule of thumb: if you have JSON, the only real thing you can do with it is Parse.
  • No, not passing JSON but still not understanding

    If SRP_Json(custHandleX, "New", "Array") then address = "Level 3":vm$:"21 Smith St" Result = SRP_Json(custHandleX, "AddValueArray", address, @VM) SRP_Json(custHandle, "Set", "site_address", custHandleX) SRP_Json(custHandleX, "RELEASE") end
    Results in
    "customer": { "site_address": [ [ "Level 3", "21 Smith St" ] ], }
  • edited January 8
    AddValueArray is meant as a convenient shortcut to add an array of values to an array. In this case, you want to call SetValueArray to add an array of values to an object member called site_address:

    address = "Level 3":vm$:"21 Smith St" SRP_Json(custHandle, "SetValueArray", "site_address", address, @VM)

    AddValueArray and SetValueArray are parallel to AddValue and SetValue. They add or set values without having to do a New..Release.
  • Thanks Kev. That's what I missed; the existence of SetValueArray.
  • Ahh. Yep. Makes sense now. Thanks All :)
Sign In or Register to comment.