Convert to string in Postman? - json

In Postman, I use {{$randomUUID}} as a parameter, but I need to pass it as string (text) in the request. However, there is no convert function in Postman and I could not found any proper way to convert this generated UUID to string. So, how can I convert it to string in Postman and pass it as string?
I also tried something like "menuId": {{"" + $randomUUID}},, but is does not work.

{
"menuId": "{{$randomUUID}}"
}
you just have to enclose it with double quotes

Related

JSONPath: Get field starting with # in escaped json string

I want to get a nested field in a json string using JSONPath.
Take for example the following json:
{
"ID": "2ac464eb-352f-4e36-8b9f-950a24bb9586",
"PAYLOAD": "{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
}
If I want to extract the #type field, I expect to do it like this
$.PAYLOAD.#type
But that doesn't seem to work..
Also tried this:
$.PAYLOAD['#type']
Do I need to use escape chars or something?
Posting my comment as an answer
"{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
Isn't JSON, it's a string containing encoded JSON.
Since JsonPath can't decode such string, you'll have to use a language of your desire to decode the string.
Eg: Decoding JSON String in Java

How to pass JSON string in the JSON body of an HTTP request?

How can i pass a JSON string in the JSON body of the HTTP request? The request is sent by ServiceNow to Azure Devops to set the content of a pipeline variable.
The Json body is as below:
{
"resources":{
"repositories":{
"self":{
"refName":"refs/heads/master"
}
}
},
"variables":{
"request":{
"value":"{"key1": "value1"}" #here, i declare the json string
}
}
}
"{"key1": "value1"}" is the json string that i want to pass (this is just a sample of the string).
I have tried backslash '' in front of the braces. "\{"key1": "value1"\}" It didn't work.
I have tried to put the braces between single or double quotations. "'{'"key1": "value1"'}'" It didn't work.
Do you have any idea? Maybe it is doable with the ServiceNow's language but i am not expert of it. As Azure Devops only accepts strings as pipeline variables, i have to send the json as a string.
You have to escape the double quotes of the value:
{\"key1\": \"value1\"}

Ruby parse string to json

So I have some json that looks like this, which I got after taking it out of some other json by doing response.body.to_json:
{\n \"access_token\": \"<some_access_token>\",\n \"token_type\": \"Bearer\",\n \"expires_in\": 3600,\n \"id_token\": \<some_token>\"\n}\n"
I want to pull out the access_token, so I do
to_return = {token: responseJson[:access_token]}
but this gives me a
TypeError: no implicit conversion of Symbol into Integer
Why? How do I get my access token out? Why are there random backslashes everywhere?
to_json doesn't parse JSON - it does the complete opposite: it turns a ruby object into a string containing the JSON representation of that object is.
It's not clear from your question what response.body is. It could be a string, or depending on your http library it might have already been parsed for you.
If the latter then
response.body["access_token"]
Will be your token, if the former then try
JSON.parse(response.body)["access_token"]
Use with double quotes when calling access_token. Like below:
to_return = {token: responseJson["access_token"]}
Or backslashes are escaped delimiters and make sure you first parse JSON.

How to post values from RestAssured to JSon and get a value from it?

{"service_request": {"service_type":"Registration","partner":"1010101","validity":1,"validity_unit":"Year","quantity":1,"center":"1020301","template":"2020301"}}
I have JSON values as above obtained from Request Payload, when I post data from the web page. I want to post this data from RESTAssured in java and want to get the returned value? How can I do that?
I would recommend to check https://github.com/jayway/rest-assured/wiki/Usage#specifying-request-data
I imagine you are trying to do a POST using Rest-Assured.For that simplest and easy way is.. Save your payload as String. If you have payload as JSONObject, use toJSONString() to convert it to String
String payload = "yourpayload";
String responseBody = given().contentType(ContentType.JSON).body(payload).
when().post(url).then().assertThat().statusCode(200).and()
extract().response().asString();
Above code, post your payload to given Url, verify response code and convert response body to a String. You can do assertion without converting to String as well like
given().contentType(ContentType.JSON).body(payload).
when().post(url).then().assertThat().statusCode(200).
and().assertThat().body("service_request.partner", equalTo("1010101"));

Web API JSON string result contains double quotes around value for dynamic object

My controller is as follows...
public IHttpActionResult GetData()
{
IEnumerable<dynamic> result = api.getData();
string json = JsonConvert.SerializeObject(result);
return Ok(json);
}
returns raw text from fiddler
{"#odata.context":"https://localhost:44305/api/$metadata#Edm.String","value":"[\r\n {\r\n \"UserName\": \"test#gmail.com\"\r\n }\r\n]"
You notice the JSON object for value has double quotes around it and the special characters \r\n. How do I get it to return pure JSON format???
I am not getting more details from your code but have you tried this way.
Is there any specific reason to use a serialized object?
[System.Web.Http.HttpGet]
public IEnumerable<XYZ> GetData()
{
return api.GetData();
}
Hope this helps!!!
It's by design since you are returning a json string..
Do this in js: JSON.parse(response.data)