Layer7 API Management

  • 1.  How to add JSON tags in an JSON array

    Posted May 18, 2018 03:27 AM

    Hi All,

     

    I am using API gateway 9.2.I need to add the JSON tag in all arrays and value should be unique in each array and send it in response.Below is my policy ,used Run Assertion for each time to get the JSON array response.But in response need to append 3 more tags in each array with unique value.Tried by using add XML element assertion but i am unable to add the XML element in all elements(value should be unique in each element).

     

    Note : In the below sample response ,EWB ,date and valid are the added JSON tags.In that EWB value should be unique number.

     

    Request you to please help on the above query.

     

    Payload in setcontext variable and hardcoded parameters which is not the actual requirement

    ${formattedResponse}
    {
    docno:${docno.results[0]},
    doctype:${doctype.results[0]},
    docDate: ${docdate.results[0]},
    "EWB":"1234567",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }

     

    Sample request Payload 

    {
    "invoices": [{
    "supplyType": "O",
    "subSupplyType": "1",
    "subSupplyDesc": "",
    "docType": "INV",
    "docNo": "123-8",
    "docDate": "15/12/2017"

    },
    {
    "supplyType": "O",
    "subSupplyType": "1",
    "subSupplyDesc": "",
    "docType": "INV",
    "docNo": "123-8",
    "docDate": "15/12/2017"

    },
    {
    "supplyType": "O",
    "subSupplyType": "1",
    "subSupplyDesc": "",
    "docType": "INV",
    "docNo": "123-8",
    "docDate": "15/12/2017"

    }
    ]
    }

     

    Getting Response:

    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"1234567",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }
    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"1234567",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }
    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"1234567",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }

     

    Expected Response :

     

    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"any unique number",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }
    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"any unique number",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }
    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"any unique number",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }

     

     

    Thanks & regards,

    Ansar



  • 2.  Re: How to add JSON tags in an JSON array
    Best Answer

    Broadcom Employee
    Posted May 20, 2018 08:49 PM

    It seems you are already using the correct logic -- add new element(s) inside the loop. As EWB is unique, you just need to generate EWB inside the loop. for example, you can use Generate UUID assertion for EWB before set the formattedResponse.



  • 3.  Re: How to add JSON tags in an JSON array

    Posted May 21, 2018 02:07 AM

    Hi,

     

    As suggested, used the Generate UUID assertion before formatted response variable,stored in ${unique} and set the variable in final response.It is generating unique variable but the same variable in all the arrays.But my requirement is the EWB variable should be different in all arrays.

     

    Set Context variable:

     

    ${formattedResponse}
    {
    docno:${docno.results[0]},
    doctype:${doctype.results[0]},
    docDate: ${docdate.results[0]},
    "EWB":"${unique}",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }

     

    Response :

    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"7594fb1a-7473-4253-a513-f2fc369cbcdb",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }
    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"7594fb1a-7473-4253-a513-f2fc369cbcdb",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }
    {
    docno:123-8,
    doctype:INV,
    docDate: 15/12/2017,
    "EWB":"7594fb1a-7473-4253-a513-f2fc369cbcdb",
    "date":"11/05/2018"
    "valid":"11/06/2018"
    }

     

    Thanks & Regards

    Ansar



  • 4.  Re: How to add JSON tags in an JSON array

    Broadcom Employee
    Posted May 21, 2018 06:57 PM

    Did you put Generate UUID assertion inside the loop? ie. under the Run Assertion for each item folder.

    each time the Generate UUID assertion is executed, it generates a new UUID.



  • 5.  Re: How to add JSON tags in an JSON array

    Posted May 22, 2018 02:04 AM

    Hi ,

     

    Thanks You!

    Now it is resolved.Initially i used Generate UUID outside the loop so it giving same value in all arrays.After using it inside the loop,it worked.

     

    Thanks & regards,

    M AnsarBaig