Search code examples
apigeemedical

Nested Json - Extract Data Policy


I am having trouble using the "extract data" policy for larger, nested json results. how would I write this in the apigee "extract data" policy? In particular, I'm interessted in the npi, last name, first name and business address fields. Thanks.

{
"meta": {
    "rowCount": 1
},
"result": [
    {
        "npi": 1671836487,
        "type": "individual",
        "last_name": "HUTTER",
        "first_name": "JOHN",
        "name_prefix": "MR.",
        "credential": "MD",
        "business_address": {
            "address_line": "1MAINSTREET",
            "city": "HARTFORD",
            "state": "CT",
            "zip": "06106",
            "country_code": "US",
            "phone": "8605551212",
            "fax": "8605551212"
        },
        "practice_address": {
            "address_line": "1MAINST",
            "city": "HARTFORD",
            "state": "CT",
            "zip": "06106",
            "country_code": "US",
            "phone": "8605551212",
            "fax": "8605551212"
        },
        "enumeration_date": "2013-04-03T00: 00: 00.000Z",
        "last_update_date": "2013-04-03T00: 00: 00.000Z",
        "gender": "male",
        "provider_details": [
            {
                "healthcare_taxonomy_code": "101Y00000X",
                "license_number": "002295",
                "taxonomy_switch": "yes"
            }
        ],
        "sole_proprietor": "no"
    }
]

}


Solution

  • Make sure your content-type header is set to application/json so the JSONPath option in the ExtractVariable policy works.

    Here's a sample policy to extract the above (verified in my own system):

    <ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1">
      <DisplayName>Extract Variables 1</DisplayName>
      <FaultRules/>
      <Properties/>
    
      <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
      <JSONPayload>
        <Variable name="result">
            <JSONPath>$.result</JSONPath>
        </Variable>      
        <Variable name="npi">
            <JSONPath>$.result[0].npi</JSONPath>
        </Variable>
        <Variable name="last_name">
            <JSONPath>$.result[0].last_name</JSONPath>
        </Variable>
        <Variable name="first_name">
            <JSONPath>$.result[0].first_name</JSONPath>
        </Variable>
      </JSONPayload>
      <Source clearPayload="false">request</Source>
      <VariablePrefix>json</VariablePrefix>
    </ExtractVariables>
    

    And a sample AssignMessage policy to help you troubleshooting. If you add this to a proxy with no target it'll send back a simple response with the extracted values:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
      <DisplayName>Assign Message 1</DisplayName>
      <FaultRules/>
      <Properties/>
    
      <Set>
        <Payload>
          npi={json.npi},
          last_name={json.last_name},
          first_name={json.first_name}
        </Payload>
      </Set>
    
      <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
      <AssignTo createNew="false" transport="http" type="response"/>
    </AssignMessage>
    

    Sample response from the above:

          npi=1671836487,
          last_name=HUTTER,
          first_name=JOHN
    

    references:

    http://apigee.com/docs/api-services/content/extract-message-content-using-extractvariables http://apigee.com/docs/api-services/content/generate-or-modify-messages-using-assignmessage