Extracting multiple entities requried from response

Hi guys, I have an simple proxy which has an GET resource, it gets specific records from BaaS based on {number} uri.

Now I used extract variable and assign message policy and I get the response such as,

{
  "action" : "get",
  "application" : "f01f5800-bd8a-11e4-9130-1f4f85d2c1c6",
    "path" : "/test_colls",
  "uri" : "https://api.usergrid.com/siddharth1/sandbox/test_colls",
  "entities" : [ {
    "uuid" : "31cb470a-6b41-11e5-a381-290d0c0a4018",
    "type" : "test_coll",
      "date" : "9July",
       "number" : "1",
    "username" : "Elias"
  }, {
    "uuid" : "345cee6a-6b41-11e5-95c8-db2c6459256c",
    "type" : "test_coll",
    "date" : "10July",
    "number" : "1",
    "username" : "Elias"
  } ],
  "organization" : "siddharth1",
  "applicationName" : "sandbox",
  "count" : 2
}

Now I only want two fields such as number & username from each entity. So I used extract variable and javascript policy in response to get them.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extracting-Specific-Data">
    <DisplayName>Extracting Specific Data</DisplayName>
        <JSONPayload>
         <Variable name="name">
            <JSONPath>$.entities[0].username</JSONPath>
        </Variable>        
        <Variable name="mobile_number">
            <JSONPath>$.entities[0].number</JSONPath>
        </Variable>
  </JSONPayload>
</ExtractVariables>



JavaScript

response.content = '';
response.headers['Content-Type'] = 'application/json';

// Create a brand-new JSON object for the response and fill it up
var r = response.content.asJSON;

var data = new Object();


data.name              = context.getVariable('username');
data.number     = context.getVariable('number');

r.specificData     = data;

I'm only getting one record because I'm using $.entities[0] . How do I get multiple entities(2 or more)?

Hi @Anil Sagar any tips on this?

When I use $.entities.[*], I get this response

 {
  "specificData": {
    "number": [
      "1",
      "1"
    ],
    "username": [
      "Elias",
      "Elias"
    ]
  }
}


How to get a resposne such as,


{
  "specificData": [
    {
      "username": "Elias",
      "mobile_number": "1"
    },
    {
      "username": "Elias",
      "mobile_number": "1"
    }
  ]
}
Solved Solved
0 5 1,219
1 ACCEPTED SOLUTION

@Barahalikar Siddharth ,

You need to do other way, Javascript Policy & Assign Message policy.

  • Extract entities information using javascript policy.
  • Construct new response payload and assign it to a variable using context.setVariable
  • Change the response payload using Assign Message policy using the new response payload constructed in javascript policy.

Cheers,

Anil Sagar

View solution in original post

5 REPLIES 5

@Barahalikar Siddharth ,

You need to do other way, Javascript Policy & Assign Message policy.

  • Extract entities information using javascript policy.
  • Construct new response payload and assign it to a variable using context.setVariable
  • Change the response payload using Assign Message policy using the new response payload constructed in javascript policy.

Cheers,

Anil Sagar

Thanks @Anil Sagar I have used JS Policy to get whats required. Now I have another issue.

I know that,query results are limited to 10 at a time. I have used ?limit=20, but it doesnt work.

Any idea why?

@Barahalikar Siddharth , It should work out of the box, if it's not please post another question with more details. No cross posting please.

@Anil Sagar ,Actually the limit functions works properly if I use it normally such as,

org-test.apigee.net/app/collection?limit=20, but after getting the response from JS

org-test.apigee.net/app/collection/{number}?limit=20 doesnt work. It only displays 10.

Note:I posted it here because it happens for this usecase itself.

I have attached a sample proxy, use below URL, there are actually 13, cat_05 items in collections,

http://siddharth1-test.apigee.net/cats-10/cat_05

@Barahalikar Siddharth , Attachments doesn't work in comments due to a bug. I would suggest to post new question with more details. It will be helpful for others too. Question in this page talks about extracting multiple entities from response, don't want to confuse Google with other issue with SEO 🙂 If above answer solves your original issue please accept same so that it will be helpful for others.