Topic
  • 2 replies
  • Latest Post - ‏2019-08-18T09:37:23Z by HermannSW
Jaango
Jaango
283 Posts

Pinned topic json identity transformation using gatewayscript with changes

‏2019-08-16T09:41:49Z | gatwayscript json

Hi,

 

Here is the input json that I have as below

 

{  
   "request":{  
      "requestTag":{  
         "trigger":"abc",
         "datapoweruser":"german"
      },
      "enduser":{  
         "first":"mansoor",
         "last":"ali",
         "gender":"male",
         "siblings":[],
         "values":[  
            {  
               "ab":"alphabets"
            }
         ]
      }
   }
}

I would like to identify the empty arrays(siblings here), it should work in a generic way and output the json as below(which will tell the empty array by adding a key value. So the out put expected is. Please how to achieve this using gatewayscript?

 

{  
   "request":{  
      "requestTag":{  
         "trigger":"abc",
         "datapoweruser":"german"
      },
      "enduser":{  
         "first":"mansoor",
         "last":"ali",
         "gender":"male",
         "siblings":[  
            {  
               "array":"empty"
            }
         ],
         "values":[  
            {  
               "ab":"alphabets"
            }
         ]
      }
   }
}

 

  • HermannSW
    HermannSW
    8694 Posts
    ACCEPTED ANSWER

    Re: json identity transformation using gatewayscript with changes

    ‏2019-08-18T09:37:23Z  
    • swlinn
    • ‏2019-08-16T18:24:36Z

    You'll need to write a recursive function that looks at the type of each object's properties.  If the object's property is a primitive (not an object or array, for example a string, number or boolean)  then there is no recursion to do.  If the object property is an object, then you call the function recursively, passing in the object property object.  If an array, in your case you can check if the array has no elements, and if so, change it's value to "empty", but if it isn't empty, you'll need to iterate over every element.  If that element is not a primitive, then recursively call the function passing in the array element. 

    Regards,
    Steve

    Steve is exactly right.

    XML Dom (in GatewayScript or DataPower XSLT) is immutable.

    Unlike that JSON is mutable, and you can find a nice and nontrivial recursive function you can start from in this forum posting:
    https://www.ibm.com/developerworks/community/forums/html/topic?id=569ae68d-65df-4e7f-ba76-5b630cef2ff5&permalinkReplyUuid=bc29dcee-a88d-4277-9464-ef6a4548702c

    Hermann.

  • swlinn
    swlinn
    1446 Posts

    Re: json identity transformation using gatewayscript with changes

    ‏2019-08-16T18:24:36Z  

    You'll need to write a recursive function that looks at the type of each object's properties.  If the object's property is a primitive (not an object or array, for example a string, number or boolean)  then there is no recursion to do.  If the object property is an object, then you call the function recursively, passing in the object property object.  If an array, in your case you can check if the array has no elements, and if so, change it's value to "empty", but if it isn't empty, you'll need to iterate over every element.  If that element is not a primitive, then recursively call the function passing in the array element. 

    Regards,
    Steve

  • HermannSW
    HermannSW
    8694 Posts

    Re: json identity transformation using gatewayscript with changes

    ‏2019-08-18T09:37:23Z  
    • swlinn
    • ‏2019-08-16T18:24:36Z

    You'll need to write a recursive function that looks at the type of each object's properties.  If the object's property is a primitive (not an object or array, for example a string, number or boolean)  then there is no recursion to do.  If the object property is an object, then you call the function recursively, passing in the object property object.  If an array, in your case you can check if the array has no elements, and if so, change it's value to "empty", but if it isn't empty, you'll need to iterate over every element.  If that element is not a primitive, then recursively call the function passing in the array element. 

    Regards,
    Steve

    Steve is exactly right.

    XML Dom (in GatewayScript or DataPower XSLT) is immutable.

    Unlike that JSON is mutable, and you can find a nice and nontrivial recursive function you can start from in this forum posting:
    https://www.ibm.com/developerworks/community/forums/html/topic?id=569ae68d-65df-4e7f-ba76-5b630cef2ff5&permalinkReplyUuid=bc29dcee-a88d-4277-9464-ef6a4548702c

    Hermann.