How I can convert JSON to payload of HTTP GET string? - json

I am trying to get data using GDC portal api. As it is shown in GDC API, to send an HTTP GET request, we need to have a format similar to:
https://api.gdc.cancer.gov/files?filters=%7B%22op%22%3A%22and%22%2C%22content%22%3A%5B%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22cases.submitter_id%22%2C%22value%22%3A%5B%22TCGA-CK-4948%22%2C%22TCGA-D1-A17N%22%2C%22TCGA-4V-A9QX%22%2C%22TCGA-4V-A9QM%22%5D%7D%7D%2C%7B%22op%22%3A%22%3D%22%2C%22content%22%3A%7B%22field%22%3A%22files.data_type%22%2C%22value%22%3A%22Gene%20Expression%20Quantification%22%7D%7D%5D%7D&format=tsv&fields=file_id,file_name,cases.submitter_id,cases.case_id,data_category,data_type,cases.samples.tumor_descriptor,cases.samples.tissue_type,cases.samples.sample_type,cases.samples.submitter_id,cases.samples.sample_id,analysis.workflow_type,cases.project.project_id,cases.samples.portions.analytes.aliquots.aliquot_id,cases.samples.portions.analytes.aliquots.submitter_id&size=1000
Which is a format from json payload as:
{
"filters":{
"op":"and",
"content":[
{
"op":"in",
"content":{
"field":"cases.submitter_id",
"value":[
"TCGA-CK-4948",
"TCGA-D1-A17N",
"TCGA-4V-A9QX",
"TCGA-4V-A9QM"
]
}
},
{
"op":"=",
"content":{
"field":"files.data_type",
"value":"Gene Expression Quantification"
}
}
]
},
"format":"tsv",
"fields":"file_id,file_name,cases.submitter_id,cases.case_id,data_category,data_type,cases.samples.tumor_descriptor,cases.samples.tissue_type,cases.samples.sample_type,cases.samples.submitter_id,cases.samples.sample_id,analysis.workflow_type,cases.project.project_id,cases.samples.portions.analytes.aliquots.aliquot_id,cases.samples.portions.analytes.aliquots.submitter_id",
"size":"1000"
}
I wanna add more fields to the payload json and get the equivalent http query string format. I have tried some online convertors, like: JSON-to-String Convertor. Copying the above JSON gives me the below result:
"{\"filters\":{\"op\":\"and\",\"content\":[{\"op\":\"in\",\"content\":{\"field\":\"cases.submitter_id\",\"value\":[\"TCGA-CK-4948\",\"TCGA-D1-A17N\",\"TCGA-4V-A9QX\",\"TCGA-4V-A9QM\"]}},{\"op\":\"=\",\"content\":{\"field\":\"files.data_type\",\"value\":\"Gene Expression Quantification\"}}]},\"format\":\"tsv\",\"fields\":\"file_id,file_name,cases.submitter_id,cases.case_id,data_category,data_type,cases.samples.tumor_descriptor,cases.samples.tissue_type,cases.samples.sample_type,cases.samples.submitter_id,cases.samples.sample_id,analysis.workflow_type,cases.project.project_id,cases.samples.portions.analytes.aliquots.aliquot_id,cases.samples.portions.analytes.aliquots.submitter_id\",\"size\":\"1000\"}"
So, I tried to get data using the generated string by adding
https://api.gdc.cancer.gov/files?
to the beginning of that. But, running this, gives me a different output. So, how can I convert the JSON to exactly what is given in GDC website? Sorry, I am very new to API and their usages.

Related

Regex for JMeter JSON Extractor

I'm using JMeter to validate some HTTPS requests, and for that, I'm using one JSON extractor and I want to extract data from the JSON response I'm getting. So from the below payload, I want to extract oper_state from any resource where service_name is equal to "Japan-1-3-12_service".
{
"resource":[
{
"id":"de04c6b1-a5a3-11ec-a02b-765e38f104a5-19",
"name":"Tokyo-1-1-10",
"service_name":"Tokyo-1-1-10_service",
"oper_state":"UP",
"type":"admin"
},
{
"id":"me05c6b1-a903-11ec-a02b-764313f104a5-19",
"name":"Japan-1-3-12",
"service_name":"Japan-1-3-12_service",
"oper_state":"UP",
"type":"admin"
},
{
"id":"5e04c691-a5a3-11ec-a02b-765e38f3q4a5-19",
"name":"France-1-1-3",
"service_name":"France-1-1-3_service",
"oper_state":"DOWN",
"type":"admin"
}
]}
I was using "$.resource[?(#.service_name=="Japan-1-3-12_service")].operational_state", but not getting any output in the variable.
Why you're using operational_state when in your JSON response it's oper_state everywhere?
Just change your JSON Path query to
$.resource[?(#.service_name=="Japan-1-3-12_service")].oper_state
and it should start working as expected
More information: JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

How to parse Json with Array list

I have below response from an API get call.
How do i read the value of dummyId and testcontactId. Below is the sample response:
{
"pageCount":1,
"pageIndex":0,
"pageSize":200,
"totalCount":1,
"dto":{
"data":[
{
"callDuration":"0:00:07",
"dummyId":20,
"testcontactId":"3002",
"id":54
}
],
"columns":[
{
"fieldName":"test1",
"displayName":"test Id",
"show":true,
"sortable":true,
"fieldType":"string",
"order":1
}
]
}
}
I can't place a comment to ask what language you're using, Here is how you'd do it in JavaScript
Parse your API Result to a Object with var data = JSON.parse() JSON.parse()
Then you can just go data.dto.data[0].dummyId
Since dto.data is an array, You can just use the indexes to access each option.
if you need to find specific values, You might want to take a look at Array.find
In the future, Please add a tag with your programming language and things you've tried.

How to parse dynamic json reponse and get specific value and pass it as an input to next request

I get .json file as a response from an API and from that file I should parse and fins specific parameter and pass it as an input to the next request, how do I do that using Katalon.
If I say
response = JSON.parse("response.json");
it says it is unable to identify JSON as valid. Can someone help me out with the solution?
Your JSON is invalid, maybe it is a copy-paste issue.
The valid JSON should be
{
"responseStatusCode": "OK",
"data": {
"screenName": "employeeTimeslip",
"screenType": "Redirect",
"searchResultCount": 0,
"rows": [],
"tabs": [],
"searchParams": {
"employeeID": "000092926",
"timeslipNumber": "201900019701"
}
}
}
So, you were missing a "," between "OK" and "data" and two closing curly braces at the end of the file.
You can check JSON files for validity yourself using online JSON validators, for example, this one.
i found a way to read specific parameter from the json response file like below:
val scn = scenario("ClaimSubmission")
.exec(http("request_2")
.post("URL")
.headers(headers_2)
.body(RawFileBody("json file path"))
.check(jsonPath("$..timeslipnumber").find.saveAs("timeslipnumber")))
Timeslip number would be retrieved using : .check(jsonPath("$..timeslipnumber").find.saveAs("timeslipnumber")))

freeradius 3.0.17 rlm_rest parsing json response

I'm trying to authenticate RADIUS Requests against a RESTful API (provided by Customer) using rlm_rest.
The problem I am facing is that
response JSON format (of REST API provided by Customer), is different from rlm_rest default format (indicated in etc/raddb/mods-enabled/rest).
My Virtual Server configuration as below:
Default
authorize {
...
...
rest
if (ok) {
update control {
Auth-Type := rest
}
}
}
mods-enabled/rest
authorize {
uri = "https://3rd-party-API/auth"
method = 'post'
body = 'json'
chunk = 0
tls = ${..tls}
data = '{
"code": 1,
"identifier": %I,
"avps": {
"User-Name": ["%{User-Name}"],
"NAS-IP-Address": ["%{NAS-IP-Address}"],
"Called-Station-Id": ["%{Called-Station-Id}"],
"Calling-Station-Id": ["%{Calling-Station-Id}"],
"NAS-Identifier": ["%{NAS-Identifier}"]
}
}'
}
Result
/sbin/radiusd -Xxx
HTTP response code
200
JSON Body
{
"code": "2",
"identifier": "91",
"avps": {
"Customer-Attributes": "Hello"
...
...
"Acct-Interim-Interval": "300"
}
}
The JSON structure is different from the example, and xlat parse
"code"
"identifier"
"avps"
And, of course, xlat finds no attributes match with the dictionary, while it cannot find "avps" and won't dig deeper.
So I was wondering is there anyway to either
Define the response JSON structure for xlat to parsing
Insert a "is_json" or "do_xlat" flag into the JSON ("avps"), and hope xlat will then dig deeper
Save the JSON and parse with exec/rlm_exec (using JQ or any other bash/JSON tools)
Please advise if there is any workaround. Thanks!
In FreeRADIUS version 4, there's a rlm_json module, which implements a custom node query language based on xpath (jpath), it is extremely limited and only supports some very basic queries (feel free to enhance it via PR :) ).
Below is an example I pulled out of my library of customer configurations. You can see here it's pulling out two keys (externalID and macAddress) from the root level of the JSON doc and assigning them to a couple of custom attributes (Subscriber-ID and Provisioned-MAC).
map json "%{rest_api:https://${modules.rest[rest_api].server}/admin/api/${modules.rest[rest_api].api_key}/external/getDeviceBySerialNumber?certificateSerialNumber=%{lpad:&TLS-Client-Cert-Serial 40 0}}" {
&Subscriber-ID := '$.externalId'
&Provisioned-MAC := '$.macAddress'
}
The xlat expansion can also be modified to send HTTP body data. Just put a space after the URL and pass your custom JSON blob.

JSR223 Preprocessor give Bad request while creating json in Jmeter using CSV

I am trying to create a JSON using Groovy Json Builder in Jmeter.
Below is the code I created to get the desired JSON :
import groovy.json.JsonBuilder
def json = new JsonBuilder()
json {
"Rule":{
"__type":"AndroidSamsungDeviceRelocationRule",
"RuleId":0,
"Name":"Test",
"DeviceFamily":6,
"Targets":{
"Groups":[
{
"Id":"[{LogicalId="${logicalid1}"}]"
}
],
"Devices":[
]
},
"Priority":0,
"IsEnabled":true,
"StartDate":"/Date(${__time(,)})/",
"EndDate":null,
"Mappings":[
{
"RelocationTarget":{
"Id":"${logicalid2}",
"Name":"${__P(TargetGroups)}",
"Path":"\\'${__P(TargetGroups)}'\\",
"PathVisible":"\\'${__P(TargetGroups)}'\\"
},
"IPRange":[
source Double.parseDouble(vars.get("source"))
destination Double.parseDouble(vars.get("destination"))
]
}
],
"EnrollmentCertificateId":null,
"EnrollmentCertificateName":""
}
}
sampler.addNonEncodedArgument("",json.toPrettyString(),"")
sampler.setPostBodyRaw(true)
CSV data is as follows:
0.0.0.0,255.255.255.255
10.0.0.1,255.0.0.0
The above CSV can have multiple values from 1 to 100 IPs separated by ',' .
The code also needs to be modified as per the number of IPs rows.
The ABove CSV data needs to be there under IPRange tag and Result Should be like:
"Rule":{
"__type":"AndroidSamsungDeviceRelocationRule",
"RuleId":0,
"Name":"Test",
"DeviceFamily":6,
"Targets":{
"Groups":[
{
"Id":"[{LogicalId="${logicalid1}"}]"
}
],
"Devices":[
]
},
"Priority":0,
"IsEnabled":true,
"StartDate":"/Date(${__time(,)})/",
"EndDate":null,
"Mappings":[
{
"RelocationTarget":{
"Id":"${logicalid2}",
"Name":"${__P(TargetGroups)}",
"Path":"\\'${__P(TargetGroups)}'\\",
"PathVisible":"\\'${__P(TargetGroups)}'\\"
},
"IPRange":[
{"From":"0.0.0.0","To":"255.255.255.255"},
{"From":"10.0.0.1","To":"255.0.0.1"},
......
.....
]
}
],
"EnrollmentCertificateId":null,
"EnrollmentCertificateName":""
}
But the issue I am getting is "Bad Request" in jmeter.
I fail to see why do you need to build your request body in Groovy, it can be done purely in HTTP Request "Body Data" tab like:
Check out JMeter Parameterization - The Complete Guide for examples on using external data sources for creating data-driven JMeter tests
If you want us to help you with the setup it would be good to see the desired outcome, i.e. JSON payload you would like to generate and the code you've tried as above samples don't make any sense. Get used to check jmeter.log file - normally it contains enough information to get to the bottom of the JMeter test issue. Also I suggest reading Parsing and producing JSON article and JsonBuilder class documentation.