{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Adapter for Envoy
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • 日本語コミュニティ
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
4
Question by Ashwini Rai · Sep 20, 2017 at 01:15 PM · 5k Views apigee edgeedgejavascriptjsonjson payloadarray

How can I transform a JSON Object from target server response into a different JSON object to send to the client?

I have a requirement wherein I am getting a standard JSON response from target server and I have to create JSON object from that response and send it to client application.

The response from target server is as follows:

{
  "results":
  [
    {
      "mail": "employee@gmail.com",
      "uid": "Employee Name",
      "displayName": "Username",
      "givenName": "firstname",
      "sn": "lastname",
      "Employee Id": "12345"
    },
    {
      "mail": "employee2@gmail.com",
      "uid": "Employee2 Name",
      "displayName": "Username2",
      "givenName": "firstname2",
      "sn": "lastname2",
      "Employee Id": "123456"
    }
  ]
}

This is the response that I would like to send to the client:

{
  "apiVersion": "1.0",
  "persons": [
    {
      "Empid": "12345",
      "name": "Employee Name",
      "email": "employee1@gmail.com"
    },
    {
      "Empid": "123456",
      "name": "Employee2 Name",
      "email": "employee2@gmail.com"
    }
  ]
}

I tried to achieve this using Javascript but no success so far.

I should not edit the JSON object returned by the Target server indeed I have to create a new JSON object and set that in response.content.

Best Wishes,

Ashwini

Comment
Add comment
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Close

1 Answer

  • Sort: 
avatar image
4
Best Answer

Answer by Sai Saran Vaidyanathan   · Sep 20, 2017 at 03:59 PM

HI @AshwiniRai -

This is a GREAT usecase for a JavaScript callout.

try the following JS policy code

var respObj = response.content.asJSON;
response.content = '';
response.headers['Content-Type'] = 'application/json';
var body = response.content.asJSON;

body.apiVersion = "1.0";
body.persons = [];

var data = respObj.results;
for(var i in data)
{
     var person = {
         "Empid": data[i]["Employee Id"],
         "name" : data[i]["uid"],
         "email": data[i]["mail"]
     };
     body.persons.push(person);
}

See if this works

Comment
Add comment · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to modify and construct json payload request in Javascript policy ? 2 Answers

How do I send JSON body to my POST request in Javascript and same way do do I get JSON response in my APIGEE api proxy program 2 Answers

How to parse XML request data using XPath notations in JavaScript 1 Answer

Not able to extract values from json response 2 Answers

Not able to get body content in javascript policy 1 Answer

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2021 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Adapter for Envoy
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • 日本語コミュニティ
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Badges