Understand that, Apigee Key Value Maps (KVMs) store values as strings.
Is there any ways to directly store an array like ["name", "branch"] as a value in an Apigee KVM ?
Could not find such details in docs (just in case missing anything ?) If with an example to store an array like ["name", "branch"] as a value in an Apigee KVM and use it ?
@aramkrishna6 - you just need to stringify that. While fetching also, you will probably need to JSON parse it before using it
curl --location 'https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/environments/$ENV/keyvaluemaps/$MAP_NAME/entries/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer $TOKEN' \
--data '{
"name": "jsonEntry",
"value": "[\"foo\", \"bar\"]"
}'
I used the provided curl to make entry to KVM-
curl --location 'https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/environments/$ENV/keyvaluemaps/$MAP_NAME/entries/' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer $TOKEN' \ --data '{ "name": "jsonEntry", "value": "[\"foo\", \"bar\"]" }'
KVM has the values stored as -
{
"name": "jsonEntry",
"value": "[\"foo\", \"bar\"]"
}
When fetched the entry in KVM policy, suppose as variable "arraykey" - debug shows as ["foo","bar"].
Applied JS policy to deserialize it as below-
var Serializedkey = context.getVariable("arraykey");
context.setVariable("key",JSON.parse(Serializedkey));
getting key value as "org.mozilla.javascript.NativeArray@61ef4c02"
That's as expected, "Serializedkey" is an array.
If you want to use the values in the array you can use KVM, JavaScript and Assign Message to see the response. Given the entry created with your curl command.
<KeyValueMapOperations continueOnError="false" enabled="true" name="KV-json-array" mapIdentifier="json-array">
<Get assignTo="jsonValue">
<Key>
<Parameter>jsonEntry</Parameter>
</Key>
</Get>
<Scope>environment</Scope>
</KeyValueMapOperations>
JavaScript
var value = context.getVariable("jsonValue");
var json = JSON.parse(value);
var foo = json[0];
var bar = json[1];
context.setVariable("foo",foo);
context.setVariable("bar",bar);
Assign Message
<AssignMessage continueOnError="false" enabled="true" name="AM-response">
<Set>
<Payload contentType="application/json">
{
"jsonValue": {jsonValue},
"foo":"{foo}",
"bar":"{bar}"
}
</Payload>
<StatusCode>200</StatusCode>
</Set>
</AssignMessage>
The result:
curl -s https://$HOST/kvm-json-array | jq
{
"jsonValue": [
"foo",
"bar"
],
"foo": "foo",
"bar": "bar"
}
Array [“foo”,”bar”] can’t be stored in KVM, it gives an error invalid JSON format
Invalid JSON payload received. Expected , or } after key:value pair.\nxy\",\n \"value\": \"[\"foo\",\"bar\"]\"\n}\n
It accepts "value": "[\"foo\",\"bar\"]" as a JSON String (Serialized Array)
Apigee & Assign Message policy just accepts JSON/XML format to process.
Array should be serialized (converted to JSON String) and then applied JSON.parse to iterate through array.
Apigee doesn’t process array as a variable, throws error - org.mozilla.javascript.NativeArray@******
Is this true ? did not see that details in Apigee Limits docs, but our requirement to process array
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |