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 - Remove

I've successfully used this function to remove members of an object.

It doesn't seem to work with arrays though. It may well just be the scenario for which I am trying it.

I am using this within one of my http_services modules. Within the getsearch logic, http_resource_services returns the string correctly based on the select criteria.
However, I want to run through the results and filter them further, removing those I don't want.
So I have the standard 'getdatabaseitems' response of
{
"_embedded": {
"item": [
{
"_links":.................... etc
},
"col1": "data",
"col2": "data",
"col3": "data",
"col4": "data",
"col5": "data"
},
{
"_links":.................... etc
},
"col1": "data",
"col2": "data",
"col3": "data",
"col4": "data",
"col5": "data"
},
]
}

What I'm trying to do is remove one of the elements from the "item" array but I can't seem to make it happen.
Is it possible and if so, any hints as to the right direction?

Thanks

Comments

  • Here's some test code the successfully removes the second element. Note that, when you remove elements in an array, the array's size is not altered. Instead, the element is converted to null.

    JsonString = '{' JsonString := ' "_embedded": {' JsonString := ' "item": [' JsonString := ' {' JsonString := ' "col1": "data",' JsonString := ' "col2": "data",' JsonString := ' "col3": "data",' JsonString := ' "col4": "data",' JsonString := ' "col5": "data"' JsonString := ' },' JsonString := ' {' JsonString := ' "col1": "data",' JsonString := ' "col2": "data",' JsonString := ' "col3": "data",' JsonString := ' "col4": "data",' JsonString := ' "col5": "data"' JsonString := ' }' JsonString := ' ]' JsonString := ' }' JsonString := '}' Result = SRP_Json(Obj, "PARSE", JsonString) Succeeded = Result EQ "" If Succeeded then EmbeddedObj = SRP_Json(Obj, "GET", "_embedded") If Len(EmbeddedObj) then Array = SRP_Json(EmbeddedObj, "GET", "item") If Len(Array) then SRP_Json(Array, "REMOVE", 2) SRP_Json(EmbeddedObj, "SET", "item", Array) SRP_Json(Array, "RELEASE") end SRP_Json(Obj, "SET", "_embedded", EmbeddedObj) SRP_Json(EmbeddedObj, "RELEASE") end SRP_Json(Obj, "RELEASE") end

    As you can see, the code is not simplistic. You have to work on the object level. First I get a handle to the array. Then I remove the element, Then I pass the updated array back to the parent element and so on up the chain.
  • Kevin,

    You beat me to the punch. I was trying to see if the REMOVE method would support the path syntax like THE GETVALUE method. That would be pretty wizard...
  • Damn I was close!

    I was missing this line

    SRP_Json(Obj, "SET", "_embedded", EmbeddedObj)

    I didn't continue up the chain.

    So is there a way that I could remove the element altogether rather than returning null?

    I guess I could create a new array within the loop that only contains the elements I want and set that one to "_embedded"

    Thanks for being the sounding board
  • Just looking to avoid this result, five "null"s before I get to real data.
    I had read that remove would set to null rather than remove but now I see the output, not sure that it works for me or rather the end developer.

    {
    "_embedded": {
    "item": [
    null,
    null,
    null,
    null,
    null,
    {
    "_links": {
    "self": {
    "href": "http://localhost/api/timecards/72~wemargaret~17557~31500~57600"
    }
    },
    "authorised": "No",
    "date": "25/01/2016",
    "desc": "Normal Day",
    "empconfirmed": "Yes",
    "employee": 72,
  • :) Answered my own question.

    NewArray worked better in this scenario than remove.

    Am curious though under what conditions null elements within an array might be of value?
  • Mark,

    I don't have an answer to your question. :) But I'll throw a question your way. Why do you need to remove elements out of the array rather than just build the array correctly the first time? I understand you are getting the serialized JSON from the getdatabaseitems service, but cannot you not add another parameter to this service so you can passing additional criteria that would allow you to build the JSON object correctly the first time?
  • I was wondering when you would ask that question.

    I have asked myself the same one.

    I think to do so will end up with that service losing it's greatest advantage, that being it's generic nature.
    Instead, I am manipulating the result within the service that is data aware based on the fact that it is a service for a specific resource.
    getdatabaseitems returns the whole result using a btree.extract. The criteria for this is modified to suit before the call. This result may well be the right answer and if so can return immediately.
    Alternatively, there may be other factors that require the result to be reduced further that don't really lend themselves to a sensible select statement or btree and as such can't be sent through.

    That's the reasoning for the approach initially and time is of the essence.

    That said and now that you've mentioned it as well, I suppose I could pass some kind of generic key value pair parameter into resource_services to interrogate within the readnext loop.

    Food for thought.
  • Mark,

    I approve your sensibility in preserving the generic quality of the service for the reasons you noted. I think, however, your final comments may present a reasonable middle ground. It provides you a way to direct the service toward greater granularity without embedding context awareness.
  • Well the next question for me then is how do I create the logic to accommodate the ever growing complexity of the comparison?

    If (this = that) and ((something = somevalue) or (something else _eqc someothervalue)) into a generic parameter without losing a week from the rapidly approaching deadline.

    Can it be done? Sure.
    Worth the design time? Not today. Maybe version 2.
Sign In or Register to comment.