API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Need to Convert String to application/json

    Posted Sat April 29, 2023 06:46 AM

    Hello Team,

    We have received response from Endpoint URL in String format, but the content-type of Response is "application/json", we need to fetch that value and convert it into proper JSON format (ie in Parameter name, value) format.
    We have tried to change Content-type of response and convert it, but its shows blank json response (ie {}).
    Please help how to convert it into API Connect (V10, V5).
    Please find below example for same.

    Received Response:
    abcdefg

    Require response:
    {"response":"abcdefg)

    Thanks and Regards,
    Jyoti






    ------------------------------
    Jyoti Yadav
    ------------------------------


  • 2.  RE: Need to Convert String to application/json

    Posted Mon May 01, 2023 02:20 PM

    You can convert from the text response to a json response using a Map policy.  Just invoke the service that returns the application/text reponse and then use a Map policy like this one:

                    - map:
                        version2.0.0
                        titlemap
                        inputs:
                          message.body:
                            schema:
                              typestring
                            variablemessage.body
                            content: text/plain
                        outputs:
                          message.body:
                            schema:
                              typeobject
                              properties:
                                response:
                                  typestring
                                  nameresponse
                            variablemessage.body
                            content: application/json
                        actions:
                          - setmessage\.body.response
                            frommessage\.body


    Produces a response of:

    {"response":"this is a text response string."}



    ------------------------------
    Steve Ruscik
    ------------------------------



  • 3.  RE: Need to Convert String to application/json

    Posted Tue May 02, 2023 01:34 AM

    const finalResponse = { "response": apim.getvariable('message.body')};

    apim.setvariable('message.body', finalResponse);



    ------------------------------
    Jayprakash Yadav
    ------------------------------