Dynamic variable while sending output as JSON

Hi ,

I am sending out as below with name as student.

{ "student": { "name": "Jim", "age": 23, "gender": "Male", "department": "History", "car": "Honda" } }

Instead I would like it to be dynamic based on pathname or query param.

Example if pathname /query param is student it would be {"student": {.....})

and if it is employee it would be {"employee": {.....})

0 6 6,250
6 REPLIES 6

Hi there

How is your output currently generated? Is this coming from a backend with that structure? Should the backend be creating the structure as required? Or are you wanting to replace what's coming back from the backend?

With that said, before doing such a change some other questions I have is are

* Is there a fundamental difference between the schema of a student and an employee?

* Should your API just be returning the inner structure - name, age, etc. If one has already used a pathname that implies they're getting back a student or an employee, then is it redundant to return that in the response again?

You can use javascript policy and use json objects to modify entries. In javascript you can read the path context variables and do json modification as desired.

I am reading the path and getting the desired path example student or employee but not able to dynamically assign the same dynamically to json. Do you have an example ?

1.Not much of a difference

2.Though redundant, API needs to return the name.

I think you need some fancy JS to modify the payload. It might look like this:

var origResponse = JSON.parse(context.getVariable('response.content'));
var firstKey = Object.keys(origResponse)[0];
var pathsuffix = context.getVariable('proxy.pathsuffix');
// pathsuffix now contains  /student or  /employee

// chop the leading slash
pathsuffix = pathsuffix.slice(1);

// create a new object to hold the modified response
var newResponse = {};

// copy the value of the "first property" of the original response 
// to a property of a given name in the new response
newResponse[pathsuffix] = origResponse[firstKey];

// serialize that to a string
var s = JSON.stringify(newResponse, null, 2) + '\n';

// set that string as the modified response
context.setVariable('response.content', s);

What this does: it parses the response.content as JSON. It then extracts the first key or property name of that JSON hash. For example if the JSON is like this:

{
  "foo" : { "bar" : 1 } 
}

...then firstKey gets "foo". In your case it would be "student". The code then obtains the pathsuffix - either "student" or "employee".

Then the logic creates a new response object, using the chopped pathsuffix as the property name. The value of that property will be the value in the original response. Then that new response gets serialized to a string.

See attached proxy for a working example.

sriharsha-1-proxy.zip

NB: this may be obvious but: If your original response contains more than one property at the first level, then ... only the first property will be copied to the modified response.

@Dino

Thank you.