unable to convert raw data to json format after replacing the \(slash) - testing

I am passing the raw data and removing unwanted slash and trying to convert to json format, unable to proceed.
json spot = {"LoginDetails":"{\"firstName\":\"abcd\",\"lastName\":\"\",\"middleName\":\"\",\"phoneNumber\":\"6944000000\",\"phoneCountryCode\":\"+91\",\"dob\":\"1945-02-22\"}"}
string a = spot
def ab = a.replace("\\", "")
json st = ab
And request st
When method POST
Then status 200
Getting error
net.minidev.json.parser.ParseException: Unexpected token f
How to resolve this issue and convert the above payload into json format?
NOTE: After conversion to json firstname, phonenumber should be passed dynamically from the preceding API calls.

If u want escape slash with json, you could try
a.replaceAll("\\", "\\\\")
or change it to unicode.
a.replaceAll("\\", "\u005C")
Both of them is workable for browser but not sure for karate, maybe you need change a bit by yourself.

Related

Karate match two json files(expected json and API response) irrespective of the order of array elements

Expected Response:
{"data":{
{"assignments":[{"locationId":"1186755","locationName":"X.11.11"},{"locationId":"1186756","locationName":"X.11.12"}]}}}
Response:
{"data":{
{"assignments":[{"locationId":"1186756","locationName":"X.11.12"},{"locationId":"1186755","locationName":"X.11.11"}]}}}
I saw a SO post stating to use karate.sort(response, x=>x.locationId), when i tried it's giving me empty response. Is there any simple way i can achieve do the comparison of whole response file irrespective of order?
Note: I even tried contains only, but it's failing the assertion.
Just use contains deep: https://stackoverflow.com/a/64373344/143475
* def response = {"assignments":[{"locationId":"1186755","locationName":"X.11.11"},{"locationId":"1186756","locationName":"X.11.12"}]}}}
* match response contains deep {"assignments":[{"locationId":"1186756","locationName":"X.11.12"},{"locationId":"1186755","locationName":"X.11.11"}]}}}

ORDS: Removing escaping char from json in ORDS OUT param

I am using the OUT parameter in the ORDS parameters to send a response which is already in JSON and this stored as CLOB in the DB. When I send this out in the response from the ORDS, ORDS is actually adding lot of back spaces to the response.
Can someone help me to understand how I can remove the escaping chars here(all the backslashes). I had tried a different approach explained by Jeff in another thread to try to use an alias for the JSON key, but it did not work for me.
Over here I have mentioned the response as OUT parameter in my code.
eg payload:
{
"response": "{\n\"Order\":{\n\"OriginalShipmentID\":1\n,\"orderNumber\":1\n,\"orgID\":1\n,\"orderShipmentNumber\":1\n,\"oracleShipSet\":\"1\"\n,\"OeHeaderId\":1\n,\"GsHeaderId\":1\n,\"CustomerPoNumber\":\"1\"\n,\"PaymentTerms\":1\n,\"FreightTerms\":\"PAID\"\n,\"CurrencyCode\":\"USD\"\n,\"BillToSiteUseID\":1\n,\"ShipToSiteUseID\":1\n,\"SalesChannel\":\"DIRECT\"\n,\"HeaderKeyCode\":\"1 1\"\n,\"OrigSysDocumentRef\":\"1\"\n,\"OrderTypeCode\":\"XYZ yXYZ\"\n,\"OrderTypeID\":1\n,\"SalesRepID\":-3\n,\"IsAtgOrder\":\"N\"\n,\"OrderSource\":\"MCP\"\n,\"OdiDocSet\":\"PO\"\n,\"CsServiceSymbol\":\"XYZ\"\n,\"CsServiceFriendlyName\":\"XYZ\"\n,\"CsShipper\":\"NDC_MX\"\n,\"GsShipToAddressID\":1\n,\"GsBillFrtAddressID\":-1\n,\"BillingMethod\":\"DoNotBillFreight\"\n,\"ReasonForNoInvoice\":\"NA\"\n,\"ShippedBy\":\"NA\"\n,\"ManifestID\":1771748\n,\"IsVoided\":\"N\"\n,\"CustomerNumber\":\"1\"\n,\"OrderLines\":[\n]\n,\"HeaderShortNotes\":{\n}\n}\n}\n"
}
Some more details:
SELECT get_json ( :ordernumber,
:warehouseid,
:shipset,
:ordershipmentno,
:gsshipmentid,
:countrycode,
:shipperid,
:fromcurrency,
:tocurrency,
:groupid,
:optionvalues,
:linehaul,
:servicename,
:sigid,
:lineid,
:customerid) "{}Order"
INTO l_response_clob
FROM DUAL;
This function returns a CLOB with a JSON format data and l_response_clob is defined as STRING output parameter on the ORDS.
Essentially, I want to stop ORDS from again converting JSON to JSON.
Been banging head over this from some time but can't seem to make it work.
Thanks for the help I can get here.

how to pass multiple type of data to API from server side as formdata?

I want to create circuit (messenger like skype) conversation from my server side code (C#) by calling circuit api.
Function : https://circuitsandbox.net/rest/v2/conversations/
I have to pass two type of parameters :
1) Participants - string array
2) topic - string
According to function definition (in swagger), I have to pass them as formdata. But, when I am trying to encode the parameters
var content = new FormUrlEncodedContent(values);
It is not accepting the array string for participants list. It is expecting "key/value" pair as "string/string".
I have even tried to create the JSON serialization also
JsonConvert.SerializeObject(values)
But the API definition is not accepting this converted values as it is expecting formdata in string/string as key/value.
I had even tried to concatenate the participants list with ";" as delimiter. But in that case, I am getting 400 error.
I tried to convert my parameters like this also
var formData = new List>();
formData.Add(new KeyValuePair("participants", JsonConvert.SerializeObject( participants)));
formData.Add(new KeyValuePair("topic", "Testing1"));
But again, I a getting 400 error.
Here is my API call
var response = client.PostAsync("https://circuitsandbox.net/rest/v2/conversations/group", content);
Can someone provide me some code snippet to pass that this data to API?
Let me emphasis, I am trying from server side code (C#) and not jquery code.
as we checked together earlier today, make the participants value a comma separated string of emails and it should work

send array of URL param via karate api

does karate support send an array of parameter in URL param as one of my API was working as this way. Below was one of my API test url concept. It working fine when i do manually on postman. Not sure whether karate support this kind of format or not.
https<URL>?param={"firstname":"XXX","lastname":"XXX"....}
i have tried with param and params. however params will give me & instead of ,. while in param , it will show ?param=%7B%firstname%22%3A%22abc...
Karate can support it, I am still not clear what your request is from your question but let me try. Note that as per the HTTP spec - some special characters WILL be URL-encoded.
Try these:
And param param = '{"firstname":"XXX","lastname":"XXX"}'
And param firstName = ['XXX', 'XXX']
See this demo example for more ideas: params.feature
EDIT: if you need to create dynamic JSON that is possible, please read the docs for the set keyword. And below the JSON is being converted to a string - because that is what it looks like from your description (which I really doesn't make sense to me)
* set data
| path | value |
| firstName | 'XXX' |
| lastName | 'XXX' |
* string data = data
* param param = data

How to store a Japanese Character in json_encode?

I'm trying to save a json_encoded strings to the database but whenever i do i get this:
u30abu30bf
when saving:
カタ
how do i decode such a thing?
tried php utf8_decode and encode but i doesn't seem to work,
i also tried reading the threads below:
PHP decoding and encoding json with unicode characters
difficulty passing Japanese characters(UTF-8) via json_encode
here is a var_dump data of $flex_encoded before passing it as a parameter:
//this one seems to passing the right encoding
[{"japanese_name":"\u30ca\u30ab\u30cd"},{"japanese_name":"\u30ca\u30ab\u30cd"}]
here is the function call:
$this->updateFlexData($game_id, $flex_encoded);
here is the function for updating my db:
function updateFlexData($game_id, $json_data)
{
$arrVal = array(
'json_data'=> $json_data,
);
Db::getInstance()->autoExecute('japanese_names', $arrVal, 'UPDATE', " game_id = '".$game_id."'", false);
}
database column type is:
`json_data` longtext CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
my PHP version:
PHP Version 5.3.10
Try JSON_UNESCAPED_UNICODE
json_encode($data, JSON_UNESCAPED_UNICODE);
And instead of this \u7537\u6027\u304c\u5f7c\u5973\u306b\u7740\u3066\... you're going to get 男性が彼...
after hours of tinkering with the code i somehow made it to work:
ok its maybe not the solution but:
when i proceed to storing json_data with this content(below) decoding json_data from database results to parse error because of the extra " quotations
$japanese_name = json_encode($japanese_text); //returns "\u30ca\u30ab\u30cd"
i just removed the " from each sides to make it just \u30ca\u30ab\u30cd and then proceeded to store in the db
$jp = str_replace("\"","",$japanese_name); //returns \u30ca\u30ab\u30cd