Layer7 API Management

 View Only
  • 1.  JSON response in array format

    Posted Oct 18, 2019 08:14 AM
    Hi Team,

    Can you please help me on below requirement.

    we have requirement to format the JSON response into array structure ( highlighted structure in expected response ) , please find below response JSON and expected JSON.

    Json response
    ------------------
    {
    searchresult:{
          "address": {
               "sequenceNumber": 1,
                "sequenceNumber": 1,
                 "country": "US", 
                  "city": "NEW YORK CITY",
                   "line": "1234 ECDC ST",
                    "latitude": 40.7204,  
                      "stateProvince": "NY"
                     }
            "email":  {
                 "type": "WORK",
                  "sequenceNumber": "1",
                  "precedenceCd": "ONE",
                   "value": "abc.p.abc@xx.com"
    }
    }
    }

    Expected Response 
    -------------------------------
    {
    searchresult:{
       "address":  [{
            "sequenceNumber": 1,
            "country": "US",
            "city": "NEW YORK CITY",
             "line": "1234 ECDC ST",
            "latitude": 40.7204,
            "stateProvince": "NY"
    }]
      "email":  [{
             "type": "WORK", 
             "sequenceNumber": "1",
             "precedenceCd": "ONE", 
             "value": "abc.p.abc@xx.com" 
    }]

    }

    Appreciate your help on this.
    thanks in advance,

    --Uday.


  • 2.  RE: JSON response in array format

    Posted Oct 20, 2019 12:38 AM
    Hi Uday,

    You can use Evaluate Regular Expression with Match & Replace Option to get this output.

    Find        : ("address":)(.*?)}
    Replace  : "address":[$2}]

    Thanks !


  • 3.  RE: JSON response in array format

    Posted Oct 20, 2019 03:22 AM
    Edited by Uday Reddy Oct 20, 2019 07:10 AM
    Hi Suhas M V,
    { 
       "searchresult":{ 
          "address":[ 
             { 
                "sequenceNumber":1,
                "country":"US",
                "city":"NEW YORK CITY",
                "line":"1234 ECDC ST",
                "latitude":40.7204,
                "stateProvince":"NY"
             },
             { 
                "sequenceNumber":2,
                "country":"US",
                "city":"NEW YORK CITY",
                "line":"1234 ECDC ST",
                "latitude":49.7204,
                "stateProvince":"NY"
             }
          ],
            "email":  [ 
             { 
                "sequenceNumber":1,
                "precedenceCd":"ONE",
                "type":"WORK",
                "value":"abc@xx.com"
             },
             { 
                "sequenceNumber":7,
                "precedenceCd":"ONE",
                "type":"PERSONAL",
                "value":"xxx@gmail.com"
             }
          ]
       }
    }

    Thanks a lot for your reply
    ,
    yes, its working for one node and one entry  but we need to apply for multiple nodes in response like email and telephone  and some time response coming with array format( like below), that time its  applying two time [] brackets.




  • 4.  RE: JSON response in array format

    Posted Oct 21, 2019 12:23 AM
    Hi Uday,

    You will have to repeat this regular expression for all the fields that is expected to be an array. Also execute it logically. You can evaluate JSON Path/Xpath to get number of objects/elements & then if the count =1 , execute regular expression.


  • 5.  RE: JSON response in array format
    Best Answer

    Posted Oct 23, 2019 06:29 AM
    Hi Suhas M V,

    Thanks a lot for your help on this.

    I have resolved this issue with regex expression. the expression is applied in complete response and finding the tag which is not in array then replacing with array[].

    find :        ("email":\s*\{)(.*?)}

    Replace:    "email":[$2}]

    Thank you.
    Uday.