How to add multiple values to a single key in KVM using KVM Api

Not applicable

Hello All,

I need to have a single key with multiple values using KVM API.

I'm using below but getting error, Please check

https://xxx.x.x.com/v1/organizations/org/e/env/keyvaluemaps/


{
  "entry" : [ {
    "name" : "KEY",
    "value" : ["value1", "value2"]
  } ],
  "name" : "kvmlist"
}


Error: {
  "message" : "Can not deserialize instance of java.lang.String out of START_ARRAY token\n at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@3bd66b48; line: 3, column: 27] (through reference chain: com.apigee.keyvaluemap.resource.KeyValueMap[\"entry\"]->com.apigee.keyvaluemap.resource.KeyValueMapEntry[\"value\"])",
  "contexts" : [ ]
}
0 11 2,488
11 REPLIES 11

Hi @venkat ch - The value attribute does not accept arrays. You can save it as a String comma separated, something like this

{
  "name" : "KEY",
  "value" : "value1, value2"
}

@venkat ch , @Sai Saran Vaidyanathan , KVM Policy does support multiple values per entry similar to array values, Question is why not API ?

Hello Sai, @Anil Sagar

Thanks. It's working.

However while reading and assigning to variable in KVM i need to read specific index. In whole index also has to be variable, how to achieve it?

Normally it takes 


<Get assignTo="companyidkvm" index="1">
        <Key>
            <Parameter>companylist</Parameter>
        </Key>
    </Get>
</KeyValueMapOperations>

@venkat ch This is achievable through java script. Please refer below code. Here companyids is an array.

var companyids = companyidkvm.split(",");
print(companyids[2]);

I realized its pretty old post. But i wanted to clarify as original question was about creating multi valued KVM entry from management api in similar way as KVM policy support as below.

<Entry>
 <Key>
   <Parameter>key_name_variable</Parameter>
 </Key>
  <Value>v3</Value>
  <Value>v4</Value>
</Entry>

Will below payload create multi valued KVM or create single value with comma separated which needs to be split later on using .split(",") ?

{
  "entry" : [ {
    "name" : "KEY",
    "value" : "value1, value2"
  } ],
  "name" : "kvmlist"
}

The one you have asked will create one value separated by comma.

so there is no way to create multi valued entry using management apis as is?

As per my understanding no.

Hi @venkat ch,

You probably solved your problem, but I wanted to clarify. If you use a KVM lookup without the index, it will return the entire comma separated value.

<Get assignTo="companyidkvm">
        <Key>
            <Parameter>companylist</Parameter>
        </Key>
    </Get>
</KeyValueMapOperations>

To get a particular value use the index:

<Get assignTo="companyidkvm" index="1">
        <Key>
            <Parameter>companylist</Parameter>
        </Key>
    </Get>
</KeyValueMapOperations>

You can create the initial KVM using API:

curl -n --location --request POST 'https://api.enterprise.apigee.com/v1/o/ORG/e/ENV/keyvaluemaps' \
--header 'Content-Type: application/json' \
--data-raw '{
    "encrypted": false,
    "entry": [
        {
            "name": "k1",
            "value": "v1"
        },
        {
            "name": "k2",
            "value": "v2,v3"
        }
    ],
    "name": "demo"
}'

You can access individual values using:

<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="KVM" mapIdentifier="demo">
    <DisplayName>KVM</DisplayName>
    <Properties/>
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
    <Get assignTo="myvar1" index="1">
        <Key>
            <Parameter>k1</Parameter>
        </Key>
    </Get>
    <Get assignTo="myvar2-1" index="1">
        <Key>
            <Parameter>k2</Parameter>
        </Key>
    </Get>
    <Get assignTo="myvar2-2" index="2">
        <Key>
            <Parameter>k2</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

Or the entire value using:

<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="KVM-2" mapIdentifier="demo">
    <DisplayName>KVM-2</DisplayName>
    <Properties/>
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
    <Get assignTo="myvar2">
        <Key>
            <Parameter>k2</Parameter>
        </Key>
    </Get>
</KeyValueMapOperations>

NOTE: you cannot mix Gets of the whole and indexed values in the same policy.

For some reason the API code block isn't rendering properly.

Use:

POST .../keyvaluemaps

--data-raw '{

"encrypted": false,

"entry": [

{

"name": "k1",

"value": "v1"

},

{

"name": "k2",

"value": "v2,v3"

}

],

"name": "demo"

}'

curl -n --location --request POST 'https://api.enterprise.apigee.com/v1/o/ORG/e/ENV/keyvaluemaps' \
--header 'Content-Type: application/json' \
--data-raw '{
    "encrypted": false,
    "entry": [
        {
            "name": "k1",
            "value": "v1"
        },
        {
            "name": "k2",
            "value": "v2,v3"
        }
    ],
    "name": "demo"
}'