1

I want to ask if there is a way to extract a boolean element from a JSON response from REST API.

I have a claim that contains a JSON:

{
    "customerEntity": {
        "role": {
            "id": 1
        }
    },
    "settings": {
        "isAdmin": false,
        "isBlocked": false
    }
}

I already tried the following ClaimsTransformations: GetClaimFromJson, GetSingleItemFromJson however it returns an error during the upload to Identity Experience Framework since the expected extracted claim is a String.

Would greatly appreciate your help.

waanderer
  • 157
  • 1
  • 1
  • 11

1 Answers1

3

The simplest solution would be to map the response to a claim from the REST API technical profile.

I'll add a simple example that I used for testing

Technical Profile

      <TechnicalProfile Id="TestEchoJson">
        <DisplayName>Test Echo</DisplayName>
        <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        <Metadata>
          <Item Key="ServiceUrl">{Settings:TestApiUrl}</Item>
          <Item Key="AuthenticationType">None</Item>
          <Item Key="ResolveJsonPathsInJsonTokens">true</Item>
          <Item Key="SendClaimsIn">QueryString</Item>
        </Metadata>
        <OutputClaims>
          <OutputClaim ClaimTypeReferenceId="isAdmin" PartnerClaimType="settings.isAdmin" />
          <OutputClaim ClaimTypeReferenceId="customProperty" PartnerClaimType="settings.customProperty" />
        </OutputClaims>
      </TechnicalProfile>

The Claim schema

      <ClaimType Id="customProperty">
        <DisplayName>customProperty</DisplayName>
        <DataType>string</DataType>
      </ClaimType>
      
      <ClaimType Id="isAdmin">
        <DisplayName>isAdmin</DisplayName>
        <DataType>boolean</DataType>
      </ClaimType>

Mock Api used for testing

app.get('/echo', async (req, res) => {

    let jsonResponse = {
        "customerEntity": {
            "role": {
                "id": 1
            }
        },
        "settings": {
            "isAdmin": false,
            "isBlocked": false,
            "customProperty": "Hello there"
        },
    };

    res.json(jsonResponse);
});
sabique
  • 223
  • 1
  • 7
  • Thanks to this approach I was able to retrieve the value from JSON by mapping them to a claim. Was there any documentation about this? The only documents that I read regarding custom policies and JSON are from the ClaimsTransformations. – waanderer Jun 03 '22 at 01:40
  • @waanderer, I couldn't find the exact links to documentations for this. But it works for now. [The only reference I could find is here](https://learn.microsoft.com/en-us/azure/active-directory-b2c/restful-technical-profile), Search for the text `ResolveJsonPathsInJsonTokens` in the page – sabique Jun 08 '22 at 05:10
  • **ResolveJsonPathsInJsonTokens** : Indicates whether the technical profile resolves JSON paths. Possible values: true, or false (default). Use this metadata to read data from a nested JSON element. In an OutputClaim, set the PartnerClaimType to the JSON path element you want to output. For example: firstName.localized, or data[0].to[0].email. – sabique Jun 08 '22 at 05:16