JSON ROOT ELEMENT

I need to remove root(Root) element in the below JSON payload.Can you please suggest me how to do in Apigee.

Attached JSON payload your reference.

7949-json.jpg

Solved Solved
0 7 10.5K
1 ACCEPTED SOLUTION

@Jegath Kumar

An enhancement to Siddharth's code above, if you dont have a fixed root element - extract the root element and then get the content which you can assign to a flow variable and use elsewhere. Sample code below is assigning it to the response content.

Input:

{
  "root1": {
    "id": 3343,
    "name": "sid"
  }
}
<br>

Output:

{
    "id": 3343,
    "name": "sid"
}
var json = context.getVariable("request.content");
var obj = JSON.parse(json);
var root;
for (var prop in obj) {
    root = prop;//get the root node of the JSON
    break;
}
var rootContent = obj[root];
context.setVariable("response.content", JSON.stringify(rootContent));

View solution in original post

7 REPLIES 7

This can be done in multiple ways, for custom coding we can use Javascript policy.

I have used Assign Message policy with message template,

Sample JSON,

{
  "root": {
    "id": 3343,
    "name": "sid"
  }
}

Assign Message Policy(Assuming payload is coming in the request call),

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="AM-Response">
    <AssignVariable>
        <Name>json_path_id</Name>
        <Value>$.root.id</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>json_path_name</Name>
        <Value>$.root.name</Value>
    </AssignVariable>
    <Set>
        <Payload contentType="application/json">
            {
                "name":"{jsonPath(json_path_name,request.content)}",
                "id":{jsonPath(json_path_id,request.content)}
             }
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

Output Response,

{
    "name": "sid",
    "id": 3343
}

Thanks @Siddharth Barahalikar for your response,Just imagine payload is dynamic and large.can we achieve this by the above approach.can you please share the java script code also.Thanks in advance.

Sampel JS code,

var input_json = context.getVariable("request.content");
context.setVariable("InputJSON",input_json);


var parse_InputJson = JSON.parse( input_json );
var extracted_json = parse_InputJson.root;
context.setVariable("ExtractedJSON",JSON.stringify(extracted_json));

Here ExtractedJSON is the flow variable and can be used in other policies/conditions.

@Jegath Kumar

An enhancement to Siddharth's code above, if you dont have a fixed root element - extract the root element and then get the content which you can assign to a flow variable and use elsewhere. Sample code below is assigning it to the response content.

Input:

{
  "root1": {
    "id": 3343,
    "name": "sid"
  }
}
<br>

Output:

{
    "id": 3343,
    "name": "sid"
}
var json = context.getVariable("request.content");
var obj = JSON.parse(json);
var root;
for (var prop in obj) {
    root = prop;//get the root node of the JSON
    break;
}
var rootContent = obj[root];
context.setVariable("response.content", JSON.stringify(rootContent));

Thanks @Nagashree B ,It worked for me.

{
  "elastic": {
    "username": "elastic",
    "roles": [
      "superuser"
    ],
    "full_name": null,
    "email": null,
    "metadata": {
      "_reserved": true
    },
    "enabled": true
  },
  "kibana": {
    "username": "kibana",
    "roles": [
      "kibana_system"
    ],
    "full_name": null,
    "email": null,
    "metadata": {
      "_deprecated": true,
      "_deprecated_reason": "Please use the [kibana_system] user instead.",
      "_reserved": true
    },
    "enabled": true
  },
  "kibana_system": {
    "username": "kibana_system",
    "roles": [
      "kibana_system"
    ],
    "full_name": null,
    "email": null,
    "metadata": {
      "_reserved": true
    },
    "enabled": true
  },
  "logstash_system": {
    "username": "logstash_system",
    "roles": [
      "logstash_system"
    ],
    "full_name": null,
    "email": null,
    "metadata": {
      "_reserved": true
    },
    "enabled": true
  },
  "beats_system": {
    "username": "beats_system",
    "roles": [
      "beats_system"
    ],
    "full_name": null,
    "email": null,
    "metadata": {
      "_reserved": true
    },
    "enabled": true
  }
  
}


In this every root is different and we need only the inner elements, we want it to be in java..Please let me know if that is possible

please ask a new question, rather than posting a new question in a comment to an older, related question.