Announcements
The Google Cloud Community will be in read-only from July 16 - July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Dealing With JSON in KVM

Dear Team,

I hope you are doing well,

I have a little challenge see the below scenario.

Show More
{
"operator1": "XXXXXXXXXXXXXXX",
"operator2": "YYYYYYYYYYYYYYY",
"operator3": "ZZZZZZZZZZZZZZZ",
"operator4": "AAAAAAAAAAAAAAA",
"operator5": "BBBBBBBBBBBBBBB",
"operator6": "GGGGGGGGGGGGGGG",
"operator7": "JJJJJJJJJJJJJJJ",
"operator8": "KKKKKKKKKKKKKKK",
"operator9": "LLLLLLLLLLLLLLL"
}

I would like to set operators in KVM and the value is above JSON.

Then inside the developer app I would like to set the custom attribute name it operator and value is operator1 for example.

Then once this consumer use his developer app credentials I will take the custom attribute and pass it to json after I extract from KVM to extract specific operator password and then pass it to the backend.

The problem is it is dealing with in in flow variables as a string not json how can I solve this with out any custom code only use KVM, AssignMessage and ExtractVariables.

and I do not want to set in the request payload because my request is post and if I set the value in the request payload it will override my request JSON body.

Show More
I got an error -> {"fault":{"faultstring":"com.apigee.flow.Variables cannot be cast to java.lang.String","detail":{"errorcode":"Internal Server Error"}}}

once I extract value from flow and handle as a JSON.

Best Regards,

Amer Hijazi

Solved Solved
0 12 691
2 ACCEPTED SOLUTIONS

Since you want to use the KVM as a JSON and avoid creating multiple separate keys, here’s a creative approach you can try using AssignMessage and ExtractVariables policies.

To be honest, this approach looks a bit unusual to me—personally, I’d recommend splitting the keys for each operator or parse with a JavaScript policy for better clarity. But, if you prefer to stick with a limited set of policies, this method will work.

Step 1: Convert the KVM Value into a JSON Object

You can create an AssignMessage policy that converts the value retrieved from the KVM into a JSON object. This policy prepares the JSON path expression to extract the specific password you need based on the operatorKey, which I assume comes from the app object:

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-2">
  <DisplayName>AM-2</DisplayName>
  <Set>
    <Payload contentType="application/json">
      {private.operatorPasswords} <!-- This comes from your KVM -->
    </Payload>
  </Set>
  <AssignVariable>
    <Name>jsonPathExpr</Name>
    <Template>$.{operatorKey}</Template> <!-- Prepare the JSONPath based on the operatorKey -->
  </AssignVariable>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew="true" transport="http" type="request">operatorPasswordsObject</AssignTo> <!-- Create a new message with JSON payload -->
</AssignMessage>

 

Step 2: Extract the Password with JSONPath

Next, you can use the ExtractVariables policy to extract the password from the newly created JSON object using the JSONPath expression:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-1">
  <DisplayName>EV-1</DisplayName>
  <Properties/>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <JSONPayload>
    <Variable name="operatorPassword">
      <JSONPath>{jsonPathExpr}</JSONPath> <!-- Use the JSONPath expression to extract the password -->
    </Variable>
  </JSONPayload>
  <Source>operatorPasswordsObject</Source> <!-- Extract from the new JSON object created in the previous step -->
  <VariablePrefix>private</VariablePrefix>
</ExtractVariables>

 

This policy retrieves the specific operatorPassword based on the operatorKey and stores it in the variable private.operatorPassword.

Again, while splitting the keys would simplify things, if you’re set on keeping a single JSON, this method should meet your needs.

This approach can generate a lot of flow variables, especially ones that contain your passwords, like operatorPasswordsObject.content. It’s critical to mask these flow variables to prevent passwords from being exposed in Apigee Trace. Make sure you properly mask any variables containing sensitive information to avoid data leaks.

Let me know if this works for your use case!

View solution in original post

Dear @nmarkevich ,

Thank you for your efforts

Your answer to assign the value to the payload will override my current payload.

 <Set>
    <Payload contentType="application/json">
      {private.operatorPasswords} <!-- This comes from your KVM -->
    </Payload>
  </Set>

 I found another solution only using KVM with ExtractVariables policies as below.

AmerHijazi_0-1726299312134.png

Then I will use ExtractVariables as below :

AmerHijazi_3-1726299399415.png

I was got an error because, in the extract variable policy, I was using the wrong source.

I will set the company name inside the custom attribute to pass it automatically once the consumer uses his credentials.

 

Best Regards,

Amer Hijazi

 

 

 

View solution in original post

12 REPLIES 12