How to extract one attribute of json payload stored in a variable

Hi I have a public key in below format stored in a varible called "cached.jwk". I want to get value of kid.

Data

{
    "keys": [
        {
            "kty": "RSA",
            "alg": "RS256",
            "kid": "xyz123",
            "use": "sig",
            "e": "AQAB",
            "n": "abslkfjsflksdfjs"
        }
    ]
}

please let me know how I can do that. I tried Extract variable policy, but it is not working.

<ExtractVariables name="Extract-Variables-1">
  <DisplayName>Extract Variables-1
  </DisplayName>
  <Source>cached.jwk</Source>
  <JSONPayload>
    <Variable name="cachedkid">
      <JSONPath>$.keys..kid</JSONPath>
    </Variable>
  </JSONPayload>
</ExtractVariables>

Thank you

Solved Solved
0 8 2,142
1 ACCEPTED SOLUTION

Your policy looks correct as far as I can see. Have you done a trace to confirm cached.jwk contains the expected value and what is getting populated into cacedkid? You should be able to see this tracing the extract variables policy.

With that said looking at your json structure, keys contains an array. Is that expected? I believe your current use of extract variables would return an array of strings from your structure.

If you don't actually intend to use an array for keys, your structure would be

{ "keys": { "kty": "RSA", "alg": "RS256", "kid": "xyz123", "use": "sig", "e": "AQAB", "n": "abslkfjsflksdfjs" }  }

in which case the json path would be

$.keys.kid

View solution in original post

8 REPLIES 8

Your policy looks correct as far as I can see. Have you done a trace to confirm cached.jwk contains the expected value and what is getting populated into cacedkid? You should be able to see this tracing the extract variables policy.

With that said looking at your json structure, keys contains an array. Is that expected? I believe your current use of extract variables would return an array of strings from your structure.

If you don't actually intend to use an array for keys, your structure would be

{ "keys": { "kty": "RSA", "alg": "RS256", "kid": "xyz123", "use": "sig", "e": "AQAB", "n": "abslkfjsflksdfjs" }  }

in which case the json path would be

$.keys.kid

I am getting below error, but I can see cached.jwk has value before this extract variable policy. So, in my extract variable I mentioned source as cached.jwk variable, is that valid? or only request and response can be the source?

{ "fault": { "faultstring": "cached.jwk message is not available for ExtractVariable: Extract-Variables-1", "detail": { "errorcode": "steps.extractvariables.SourceMessageNotAvailable" } } }

ExtractVariables works on a Message.

If the variable 'cached.jwk' is of type "string", you cannot use the ExtractVariables policy on that variable. (Sorry, that's frustrating, I'm sure) .

YOU CAN use a jsonPath function in AssignMessage to extract an item from a json string. It would look like this:

<AssignMessage name='AM-extractJWK'>
  <AssignVariable>
    <Name>json_path_1</Name>
    <Value>$.keys[0].kid</Value>
  </AssignVariable>

  <AssignVariable>
    <Name>extracted_kid</Name>
    <Value>BADDBEEF</Value>
    <Template>{jsonPath(json_path_1,cached.jwk)}</Template>
  </AssignVariable>

</AssignMessage>

@Dino-at-Google I am getting "Unresolved variable : json_path_1,cached.jwk" error

show your entire policy please? BTW this Template works only in Apigee Edge Saas. Not OPDK. (Until 4.19.01)

attached is my working proxy.

apiproxy-extractkid-1.zip

ExtractVariable works with string when using jsonpath

how about

$.keys[0].kid

@Dino-at-Google I had a typo in my code. Now it is working. Thank you for all your help.