I am trying to persist a nested object in sessionAttributes. However, it seems Amazon Lex does not recognize if I send a structure like this in response:
{
"sessionAttributes": {
"previousIntent": "TCAIntent",
"prevSlots": {
"test": 1
}
},
"dialogAction": {
"type": "Delegate",
"slots": {
"firstName": null,
"lastName": null,
"rate": null,
"rentMortgage": null,
"downPayment": null,
"purchasePrice": null,
"email": null
}
}
}
When I replace {"test": 1} with a value, it works fine. Is there a way around this or possible mistake?
Error:
I have figured out that Lex does not allow storing json object or array on sessionAttributes. As a work around, I converted the json object to string and convert it back when I need to use it.
Related
I am having access to data of an elasticsearch instance using Kibana.
Within the data there is a text field which contains a string.
This string (Textfield) looks like JSON but it is not.
Here comes the difference (PLEASE FOCUS ON THE "message" key in the JSON):
If you expand the document to see the "expanded document" view with the little "opened folder" icon you can choose two tabs "Table" and "JSON".
Select "JSON".
Then you would see the follwing:
{
"_index": "kibana_sample_data",
"_type": "_doc",
"_id":"someString",
"_version": 1,
"_score": null,
"_source":{
"first_key":"some string",
"message":
"{
\"length\": 11.99,
\"percentage\": 0,
\"name\": \"foo\",
\"category\": \"bar\",
\"sub_msg\": \"M001: This is a message-code.\"
}"
}
}
As you can see the message-key value which should be JSON is just a string and all key and value informations are escaped (except for number values).
What I should see to select it easily using the filter of the GUI is a JSON like this:
{
"_index": "kibana_sample_data",
"_type": "_doc",
"_id":"someString",
"_version": 1,
"_score": null,
"_source":{
"first_key":"some string",
"message":
{
"length": 11.99,
"percentage": 0,
"name": "foo",
"category": "bar",
"sub_msg": "M001: This is a message-code"
}
}
}
What I am trying to do now is to extract the value of the key "sub_msg" using Query DSL and a regex. But I am not even able to find all matches with the correct beginning character of the message-key value (string).
In more detail the Elastic Search Query DSL part:
{
"query": {
"regexp": {
"message": "{.*"
}
}
}
But the search does not match anything.
What it does match is if I try to search for the keys in the message like this:
{
"query": {
"regexp": {
"message": "sub_msg"
}
}
}
What I can not do is to search for a respective value within the message like this:
{
"query": {
"regexp": {
"message": "M001: This is a message-code"
}
}
}
My actual Goal is to query for the log's message code like "M001:.*" within the value of the "sub_msg"-key. With those query-results I would like to create a simple dashboard.
Unfortunately I feel I am at a dead end right now.
I also tried stuff like escaping the escape characters of the value by adding some "\" to the actual backslashes within the Query DSL. This throws an exception in Kibana.
The issue is that your json-like string was stripped of special chars as it was indexed, presumably due to the default standard analyzer which is applied on text fields unless you explicitly provide another analyzer. More info here and here.
You can check what becomes of your message after the standard analyzer using this:
GET _analyze
{
"text": "{\"length\": 11.99, \"percentage\": 0, \"name\": \"foo\", \"category\": \"bar\",\"sub_msg\": \"M001: This is a message-code.\"}",
"analyzer": "standard"
}
Quick fix:
GET kibana_sample_data/_search
{
"query": {
"regexp": {
"message.keyword": "{.*"
}
}
}
or
{
"query": {
"regexp": {
"message.keyword": ".*\"M001: This is a message-code.\".*"
}
}
}
Long-term fix: figure out a way to ingest the json as it should be ingested w/o having to go to the extreme of regexing a json-like string.
I have created an API for SQS(FIFO) and its working fine but its returning Json response but I need XML response.
Below is the Json response which need to be converted before sending to client:
{
"ReceiveMessageResponse": {
"ReceiveMessageResult": {
"messages": [
{
"Attributes": null,
"Body": "hello",
"MD5OfBody": "5d41402abc4b2a76b9719d911017c592",
"MD5OfMessageAttributes": null,
"MessageAttributes": null,
"MessageId": "2497c4a7-3a0e-434c-bad8-6cb8d0fd825e",
"ReceiptHandle": "AQEBQw+v1uUf2ALjbKOea48ifVdeXemCS+qhEGLT2vb+eTRciW803NWUJKVErUqVpuNhmtS5Pkt8USxLZ4OUm4sWJhftLIdyZ29DoT8rBssPjB1gEG7xTohDqzeFsndznIudfE8uY6oQgb1769ICE4bji2DBHqzKAzAic7UhoUjGHw2NLzpE01FiPYnH/f7kR+Z47Wm5XdWFX2wOnzYtmZ1mAsnM0k0EtQG8aqQTQpoDjBx+ep0UsIrdg2r6O+VJfCTnepyDf2rF5kbfTdYRg0ngN32N9ecBq+6vVDpzg1lod/0="
},
{
"Attributes": null,
"Body": "hi",
"MD5OfBody": "49f68a5c8493ec2c0bf489821c21fc3b",
"MD5OfMessageAttributes": null,
"MessageAttributes": null,
"MessageId": "06c7a999-8764-45dc-8cf6-b4ad6f6a446a",
"ReceiptHandle": "AQEBf8Fsh4/+aydDWKpT20XDRCejNAAovF+RVPmg3yVhLCH/R728CqmoU99g13nE+yVneJxrv2zps84SUWGIyZQiCrOyKgkF3faOo4RW3wDWYwF+xb1ahr5B7EItskM1gQhyua+6PiBukQxnrfIcma1ANCngwv+tEe7uUGMblAd/NxvrGkCGHyuyON9xKx5c+V5+546jCgDzMhhqips5Bgy4kh5KSfKpSjJHYXVoa0JBKqDYFm017wxnhDiyi4pSEOdzS7K6L25n6z1rNyPWpTaB0RrHveZCqvO2WWKmoBGoKD8="
}
]
},
"ResponseMetadata": {
"RequestId": "5969d3d4-90c6-50df-b0ad-87d35b54835b"
}
}
}
By default, the API will send a response in JSON format to the client application. In order for the API to be able to send a response to the client in XML format, you will need to add the following HTTP Headers to the "Integration Request":
Accept 'application/xml'
reference
I have a json like below
{
"hasErrorResponse": "false",
"responseObject": [{
"success": true,
"errorMessage": null,
"availablity": "YES",
"errorCode": null,
"availableQtys": 1234,
"prId": "451667"
}]
}
Now using RestAssured if I do
response.then().assertThat().body("responseObject.prId", equalTo("451667"));
It's failing because it says Expected: 451667 but Got: [451667]
I guess it's because it's getting the prId attribute as an ArrayList and not as String. How do I go about validating this sceanrio?
hasItems matcher works to validate anything inside collection
I have the following response coming from a request
[
{
"id": 3767,
"sellerName": "abc",
"siteActivity": [
{
"siteId": -1,
"siteName": "example.com",
"categories": [
{
"categoryId": 79654,
"parentId": null,
"name": "Photo & Picture Frames",
"siteName": null,
"channelType": null
},
{
"categoryId": 114397,
"parentId": null,
"name": "Chests of Drawers",
"siteName": null,
"channelType": null
},
{
"categoryId": 11707,
"parentId": null,
"name": "Jewellery Boxes",
"siteName": null,
"channelType": null
},
{
"categoryId": 45505,
"parentId": null,
"name": "Serving Trays",
"siteName": null,
"channelType": null
}
]
}
]
},
{
"id": 118156,
"sellerName": "xyz",
"siteActivity": [
{
"categoryId": 45505,
"parentId": null,
"name": "Serving Trays",
"siteName": null,
"channelType": null
}
]
}
]
Now, I need to extract "id" values and "categoryId" values and send them as list in the next request body.
Currently, I am using JSON Path Extractor with the expression
$.[*].id
to get my hand on all ids and
$.[*].siteActivity.[categoryId]
for category ids.
Next, I want to use the above values and send them as parameters in request body.
Currently, I am able to extract only one id with
$.[0].id
and then assigning it to variable "id" and using the following in request body
{"ids":[{"id":"${id}"}]}
but I want to be able to send
{"ids":[{"id":"${id}"},{"id":"${id2}"}....]}
There is no limit on how many ids can be there, so I cannot hardcode and need something dynamic to do the aggregation. What kind of processor can help me here? Please add some example if you can.
I believe that you should be able to use Beanshell PostProcessor for building the request.
Given your sample data your $.[*].id JSONPath Expression should return the following values:
id=[3767,118156]
id_1=3767
id_2=118156
So basically you need to:
Determine "id" count
Populate dynamic JSON Object to send
Store it to JMeter variable for later use
In order to do so add a Beanshell PostProcessor after JSONPath Extractor and put the following code into its "Script" area
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; // necessary imports
JSONObject data2Send = new JSONObject();
JSONArray array = new JSONArray(); // instantiate JSON-related classes
int idCount = vars.get("id").split(",").length; // get "id" variables count
for (int i = 1; i <= idCount; i++) { // for each "id" variable
JSONObject id = new JSONObject(); // construct a new JSON Object
id.put("id", vars.get("id_" + i));// with name "id" and value id_X
array.add(id); // add object to array
}
data2Send.put("ids",array); // add array to "ids" JSON Object
vars.put("myJSON", data2Send.toString()); // store value as "myJSON" variable
You can refer to your {"ids":[{"id":"3767"},{"id":"118156"}]} data as ${myJSON} where required.
The approach will play for any number of "id" variables.
References:
Welcome to Json-lib
How to use json-lib
Json-lib JavaDoc
How to use BeanShell: JMeter's favorite built-in component
Should be a no brainer, but I'm can't seem to access the elements returned from Newtonsoft's json deserializer.
Example json:
{
"ns0:Test": {
"xmlns:ns0": "http:/someurl",
"RecordCount": "6",
"Record": [{
"aaa": "1",
"bbb": "2",
},
{
"aaa": "1",
"bbb": "2",
}]
}
}
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(somestring);
Stripping out the json up to the Record text, i can access the data without issue.
i.e. result.Recordcount
If i leave the json as shown above, can someone enlighten me how to access Recordcount?
All inputs appreciated. Thanks!
For those JSON properties that have punctuation characters or spaces (such that they cannot be made into valid C# property names), you can use square bracket syntax to access them.
Try this:
int count = result["ns0:Test"].RecordCount;