{ 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
1
Question by Abhinaba Nag · Dec 13, 2019 at 09:47 AM · 4.9k Views apigee edgeextractvariablesassign message policyjson payloadmodify json response

How to remove few fields from JSON array response?

I am getting a JSON response like below:

[
  {
    "id": "1",
    "name": "Abhi",
    "pan": "ABC",
    "bg": "O+"
  },
  {
    "id": "2",
    "name": "Ashish",
    "pan": "XYZ",
    "bg": "AB+"
  },
  .
  .
  .
]
and I want to remove the "pan" and "bg" field from all elements of the array in the final response.Like below:
[
  {
    "id": "1",
    "name": "Abhi"
  },
  {
    "id": "2",
    "name": "Ashish"
  },
  .
  .
  .
]
Comment
Add comment Show 3
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
avatar image Abhinaba Nag · Dec 13, 2019 at 10:16 AM 0
Link

Currently, I'm using Extract variable policy and Assign Message policy,

Extract Variable Policy code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-FullData">
    <DisplayName>EV-FullData</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="id">
            <JSONPath>$[*].id</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>apigee</VariablePrefix>
</ExtractVariables>

Assign Message Policy code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-FinalResponse">
    <DisplayName>AM-FinalResponse</DisplayName>
    <Properties/>
    <Set>
        <Payload>[{
            "id":"{apigee.id}"
        }]
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>
and I'm getting response like below:
[
    {
        "id": "["1","2"]"
    }
]
avatar image Dino-at-Google ♦♦ Abhinaba Nag   · Dec 13, 2019 at 04:25 PM 0
Link

Are these the results that you want? I'm not clear. Are you still experiencing a problem?

avatar image Abhinaba Nag Dino-at-Google ♦♦ · Dec 16, 2019 at 03:26 AM 0
Link

The response I'm getting:

[
    {
        "id": "["1","2"]"
    }
]

The response I want:

[
  {
    "id": "1",
    "name": "Abhi"
  },
  {
    "id": "2",
    "name": "Ashish"
  },
  .
  .
  .
]

Close

2 Answers

  • Sort: 
avatar image
2
Best Answer

Answer by Abhinaba Nag · Dec 19, 2019 at 05:56 AM

I have resolved this issue placing a Javascript Policy between Extract variable policy and Assign Message policy.

Inside Javascript policy, I'm taking the whole payload in a variable, converting it to an array, mapping required fields to another array, and then converting the second array to JSON object.

Extract Variable Policy code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-FullData">
    <DisplayName>EV-FullData</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="resp">
            <JSONPath>$</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>apigee</VariablePrefix>
</ExtractVariables>

Javascript Policy code:

var res = context.getVariable("apigee.resp");
var result = [];
var result = JSON.parse(res);
var newArr = result.map(item => { return {
  id: item.id,
  name: item.name
}});
var output = JSON.stringify(newArr);
context.setVariable("apigee.resp",output);

Assign Message Policy code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-FinalResponse">
    <DisplayName>AM-FinalResponse</DisplayName>
    <Properties/>
    <Set>
        <Payload>
            {apigee.resp}
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>
Comment
Add comment Show 4 · 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
avatar image Venkata Ramana Sai Charan Korlam · Dec 17, 2020 at 04:47 AM 0
Link

getting error in javascript policy at JSON parse line number 3 as invalid token

avatar image Priyadarshi Ajitav Jena Venkata Ramana Sai Charan Korlam · Dec 17, 2020 at 06:46 AM 0
Link

You need to check the response is in proper format to become json parsed. Just do a response validation.

avatar image Venkata Ramana Sai Charan Korlam · Dec 23, 2020 at 04:26 PM 0
Link

getting error like this .Cannot find function map in object [object Object]

avatar image Priyadarshi Ajitav Jena Venkata Ramana Sai Charan Korlam · Dec 24, 2020 at 04:24 AM 0
Link

Then you have to fix and make the response parsed as a json object

avatar image
1

Answer by Dino-at-Google   · Dec 16, 2019 at 07:49 PM

Ahh, I see. Sorry I had missed that.

What you are trying would work with a current jsonpath processor, if you used this as your jsonpath:

$[*].['id','name']

Unfortunately, the ExtractVariables depends on an older jsonpath library, which does not accept that syntax. So you cannot use ExtractVariables to do that, today.

In fact I don't know of a way to do that, with the currently built-in policies in Apigee. You can do it if you use a more current jsonpath library, like jayway jsonpath 2.4.0. This Java callout does it:

https://github.com/DinoChiesa/ApigeeEdge-Java-JSONPath

Configuration would be:

<JavaCallout name="Java-JSON-Path-Multiple-Fields">
  <Properties>
    <Property name='jsonpath'>$[*]['id','name']</Property>
    <Property name='source'>message.content</Property>
  </Properties>
  <ClassName>com.google.apigee.edgecallouts.jsonpath.JsonPathCallout</ClassName>
  <ResourceURL>java://edge-callout-jsonpath-20191216.jar</ResourceURL>
</JavaCallout>
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

136 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 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 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 extract value from JSON array object and assign to Assign message 1 Answer

extracts variable from URI and assignmessage 1 Answer

Extract variables from json using filter expression 5 Answers

Extract Variable JSON payload nodeset introducing escape characters 1 Answer

Best way to extract value from Json array Object and assign if we know the max value of array incoming. 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