How to get a xml attribute variable (type double) using xPath in response using Wiremock - wiremock

Request is an xml like this:
<Request>
<Account>373953192351004</Account>
<Amount>98.21</Amount>
</Request>
Response is a json and should have amount mapped from request but data type being a decimal number (not String) e.g.
{
"response": {
"status": "SUCCESS",
"amount": 98.21
}
}
i was able to do it with xPath using json mapping like this:
"amount": "{{xPath request.body '/Request/Amount/text()'}}"
but above xpath makes amount a string with double quotes in the response, like:
"amount": "98.21"
How do i make it without quotes like:
"amount": 98.21
note: I can't remove the quotes in "{{xPath... as it wouldn't be a valid json anymore.

You need to remove the double quotes from outside the {{ xPath ... }}. So:
"amount": {{xPath request.body '/Request/Amount/text()'}}

Related

How to map a json string into object type in sink transformation

Using Azure Data Factory and a data transformation flow. I have a csv that contains a column with a json object string, below an example including the header:
"Id","Name","Timestamp","Value","Metadata"
"99c9347ab7c34733a4fe0623e1496ffd","data1","2021-03-18 05:53:00.0000000","0","{""unit"":""%""}"
"99c9347ab7c34733a4fe0623e1496ffd","data1","2021-03-19 05:53:00.0000000","4","{""jobName"":""RecipeB""}"
"99c9347ab7c34733a4fe0623e1496ffd","data1","2021-03-16 02:12:30.0000000","state","{""jobEndState"":""negative""}"
"99c9347ab7c34733a4fe0623e1496ffd","data1","2021-03-19 06:33:00.0000000","23","{""unit"":""kg""}"
Want to store the data in a json like this:
{
"id": "99c9347ab7c34733a4fe0623e1496ffd",
"name": "data1",
"values": [
{
"timestamp": "2021-03-18 05:53:00.0000000",
"value": "0",
"metadata": {
"unit": "%"
}
},
{
"timestamp": "2021-03-19 05:53:00.0000000",
"value": "4",
"metadata": {
"jobName": "RecipeB"
}
}
....
]
}
The challenge is that metadata has dynamic content, meaning, that it will be always a json object but the content can vary. Therefore I cannot define a schema. Currently the column "metadata" on the sink schema is defined as object, but whenever I run the transformation I run into an exception:
Conversion from ArrayType(StructType(StructField(timestamp,StringType,false),
StructField(value,StringType,false), StructField(metadata,StringType,false)),true) to ArrayType(StructType(StructField(timestamp,StringType,true),
StructField(value,StringType,true), StructField(metadata,StructType(StructField(,StringType,true)),true)),false) not defined
We can get the output you expected, we need the expression to get the object Metadata.value.
Please ref my steps, here's my source:
Derived column expressions, create a JSON schema to convert the data:
#(id=Id,
name=Name,
values=#(timestamp=Timestamp,
value=Value,
metadata=#(unit=substring(split(Metadata,':')[2], 3, length(split(Metadata,':')[2])-6))))
Sink mapping and output data preview:
The key is that your matadata value is an object and may have different schema and content, may be 'value' or other key. We only can manually build the schema, it doesn't support expression. That's the limit.
We can't achieve that within Data Factory.
HTH.

GET with body (json format), what to do with apostrophe?

I am new to REST. I have a client who tries call an api I have running. Problem is that they use some type of builder to create the body.
In my endpoint, I expect name field in the body like this:
{
"name": [
{ "tag": "1", "value": "O'Bryan" },
...
But client sends something like this:
{
'name': [
{'tag': '1 ', 'value': 'O'Bryan'}
It works except when name has apostrophe. I am wondering if this body will still valid. If not, what can they do to fix it without using double quote(")?
This body will not be valid, you would need to escape the single quote like so \'

Passing multiple json as a payload for a request in Gatling

sample json payload:
'{
"Stub1": "XXXXX",
"Stub2": "XXXXX-3047-4ed3-b73b-83fbcc0c2aa9",
"Code": "CodeX",
"people": [
{
"ID": "XXXXX-6425-EA11-A94A-A08CFDCA6C02"
"customer": {
"Id": 173,
"Account": 275,
"AFile": "tel"
},
"products": [
{
"product": 1,
"type": "A",
"stub1": "XXXXX-42E1-4A13-8190-20C2DE39C0A5",
"Stub2": "XXXXX-FC4F-41AB-92E7-A408E7F4C632",
"stub3": "XXXXX-A2B4-4ADF-96C5-8F3CDCF5821D",
"Stub4": "XXXXX-1948-4B3C-987F-B5EC4D6C2824"
},
{
"product": 2,
"type": "B",
"stub1": "XXXXX-42E1-4A13-8190-20C2DE39C0A5",
"Stub2": "XXXXX-FC4F-41AB-92E7-A408E7F4C632",
"stub3": "XXXXX-A2B4-4ADF-96C5-8F3CDCF5821D",
"Stub4": "XXXXX-1948-4B3C-987F-B5EC4D6C2824"
}
]
}
]
}'
I am working on a POST call. Is there any way to feed multiple json files as a payload in Gatling. I am using body(RawFileBody("file.json")) as json here.
This works fine for a single json file. I want to check response for multiple json files. Is there any way we can parametrize this and get response against multiple json files.
As far as I can see, there's a couple of ways you could do this.
Use a JSON feeder (https://gatling.io/docs/current/session/feeder#json-feeders). This would need your multiple JSON files to be in a single file, with the root element being a JSON array. Essentially you'd put the JSON objects you have inside an array inside a single JSON file
Create a Scala Iterator and have the names of the JSON files you're going to use in it. e.g:
val fileNames = Iterator("file1.json", "file2.json)
// and later, in your scenario
body(RawFileBody(fileNames.next())
Note that this method cannot be used across users, as the iterator will initialize separately for each user. You'd have to use repeat or something similar to send multiple files as a single user.
You could do something similar by maintaining the file names as a list inside Gatling's session variable, but this session would still not be shared between different users you inject into your scenario.

ServiceNow em_event table additional_Info field is returning [object Object]

We are using Rest API via PowerShell (Invoke-RestMethod), in order to insert records in ServiceNow event [em_event] table with a single call, using the web service API.
We successfully inserting events to the em_event table,
but the only problem is with the additional_info field.
For some reason,
The JSON structure of my PowerShell script,
Is causing the output of additional_info, to return as an Object and Not as JSON string.
And as a result,
The values in additional_info not showing properly, but instead as [object Object]:
This is the JSON structure in my PowerShell script:
# Specify request body
$body = #"
{ "records":
[
{
"source":"MyClass",
"event_class":"$AtargetResourceType",
"resource":"$AtargetResourceType",
"node":"$AtargetResourceName",
"metric_name":"$Aname",
"type":"$AsignalType",
"severity":"$Aseverity",
"message_key":"$Aid",
"u_mc_object":"$AtargetResource",
"description":"$Adescription",
"additional_info":"{
'u_mc_object_class':'$AsourceCreatedId',
'u_mc_parameter':'$AmetricName',
'u_mc_parameter_value':'$AmetricValue'
}"
}
]
}
"#
image which you have posted is not opening. but according to your issue below line will return string value for additional_info:
($body|ConvertFrom-Json).records.additional_info
I had the same issue sending the request using Postman, I was sending the request like this:
{ "records":
[
{
"source":"BMC TrueSight",
"type":"Incident from trusight",
"severity":"1",
"description":"This is a test from WEB SERVICE API ERROR",
"additional_info":{
"status":"new",
"description":"This is a descriotion from additional information",
"category":"41",
"subcategory": "test",
"company": "test",
"business_service":"test"
}
}
]
}
and it showed [object][object] in the additional information field, what I did was sent that field as a string like this:
"additional_info": "{\"assignee_group\":\"ETSS\/UNIX99\",\"status\":\"new\",\"description\":\"This is a descriotion from additional information\",\"category\":\"41\",\"subcategory\":\"test\",\"company\":\"test\",\"business_service\":\"
}
You only need to convert the JSON into a string.

Send model id in Put Body not in url

As we know the format of PUT method in Restful should be something like this:
/api/user/{id}
and the PUT body
But is it OK if I just call /api/user and I send the {id} inside the PUT body like this: {"id": "1", "name": "myname"};