Not able to retrieve value in JSON response - json

I am working on API response validation. I am trying to get the value of the particular field in the API response for validation.
This is my API response.
{
"currentdatetime": "11",
"syncinterval_seconds": 4,
"public_keys": [
{
"system": "aa",
"pubkey": "aa"
}
],
"users": [
{
"username": "user",
"user_id": "user",
"realname": "user_10",
"emailaddress": "test#gmail.com",
"accountenabled": false,
"mustchangepassword": false,
"passwordhash": "$E6gi",
"accesslevel": [
"WORKER"
],
"expiration": "2022-11-02T16:21:52",
"site_specific": false,
"symkey": "aaaa",
"privatekey": "aaa"
},
]
}
I am trying to get username /user id from users through robot code.Like this
${response}= GET ${Base_URL}/v1/sync/users ${data} headers=${header}
${origin}= Get Json Value From String ${response.content} users
${lines} = Set Variable ${origin.content['users']['username']}
It is throwing a "type" error.
Any help will be much appreciated.

Related

How do I parse this response correctly in post man?

I have tried using the below in postman to parse am array response (further below). for some reason I dont just get the id value. and no mater what array position I enter, I get a type error of
cannot read properties of undefined (reading '0')
postman script
const response = pm.response.json();
console.log(pm.globals.get("alarm1Id", response.result.alarms[0].id));
get request response:
{
"status": "success",
"result": {
"alarms": [
{
"id": "C6707LJmR1yTzjYrfoBTxw",
"name": "Test Alarm1",
"state": "PURGED",
"isAssignedToCurrentUser": false,
"timeOfLastStateChange": "2023-01-20T01:04:38.917Z",
"type": "NORMAL",
"associatedCameras": [
"4xIx1DMwMLSwMDU2sTA2NdNLTsw1MBASmCLZ_fximWvi_v08agen8bMBAA"
],
"priority": 0,
"isNoteRequired": false,
"missedTriggers": 0,
"timeOfMostRecentAcknowledgement": "2023-01-20T01:02:09.670Z",
"timeOfMostRecentActivation": "2023-01-20T01:02:02.346Z",
"timeOfMostRecentPurge": "2023-01-20T01:04:38.917Z",
"durationOfPreRecording": "PT5S",
"durationOfPostRecording": "PT1M",
"digitalOutputsToActivate": []
},
{
"id": "kNJnCVsdQFWzvOeBshTqeg",
"name": "FR",
"state": "PURGED",
"isAssignedToCurrentUser": false,
"timeOfLastStateChange": "2022-12-01T23:26:41.482Z",
"type": "NORMAL",
"associatedCameras": [
"49Ix0UsyNU4ySEpN002yTDbSNTFONNW1NLEw001LTkw0NjI2ME-0sNBLTsw1MBASmCLZ_fximWvi_v08agen8bMBAA"
],
"priority": 0,
"isNoteRequired": false,
"missedTriggers": 0,
"timeOfMostRecentAcknowledgement": "2022-07-07T18:21:28.981Z",
"timeOfMostRecentActivation": "2022-07-06T17:17:09.751Z",
"timeOfMostRecentPurge": "2022-12-01T23:26:41.482Z",
"durationOfPreRecording": "PT5S",
"durationOfPostRecording": "PT1M",
"digitalOutputsToActivate": []
}
]
}
}
I have tried different list positions . I'm thinking I just don't fully understand that data structure on the response.
response = JSON.parse(responseBody)
alarm1Id = response.result.alarms[0].id
I did some digging, I was able to extract id data with the script above

JMeter - Extract value from the variable having JSON data

I am getting following response in one GET API call.
{
"id": 78929,
"created": "04-12-2021 02:37:52",
"modified": "04-12-2021 02:37:52",
"name": "Applebee",
"url": "https://applebee-2.prashant.com",
"key1": "vp_9d254bb754ab6283afa9ecf0f0847b5a",
"interval2": 120,
"tenant_id": 60895,
"channel_id": 2,
"channel_type": "platform",
"warehouse_id": 54533,
"warehouse_name": "Bedfordview Warehouse",
"stock_policy": "CUSTOM",
"currency_id": 45396,
"order_from_date": "2021-10-03 02:36:48",
"inventory_update": true,
"order_update": true,
"price_update": true,
"active": true,
"resync": true,
"last_access_record_id": 89880,
"last_accessed_record": {
"id": 89880,
"last_order_to_date": "2021-12-07T00:16:39.000+0000",
"last_order_id": "0",
"last_order_page": 1
},
"store_config": "{\"isOrderUpdateProcessor\":false,\"auto_archive\":true,\"tagOrderRisks\":true,\"error_emails\":\"kyle#gmail.com\",\"autoMapDuplicateProduct\":true,\"enabledMultiCurrency\":false,\"disableSyncPIIData\":false}",
"last_inventory_sync": "04-12-2021 02:43:35",
"inventory_sync_interval": 1440,
"stock_policy_warehouse_ids": [
{
"id": 53543,
"created": "04-12-2021 02:37:52",
"modified": "04-12-2021 02:37:52",
"store_id": 78929,
"external_warehouse_id": "34639577171",
"warehouse_id": 54533,
"add_qty_value": "0%",
"add_incoming_qty_value": "0%",
"tenant_id": 60895
},
{
"id": 53544,
"created": "04-12-2021 02:37:52",
"modified": "04-12-2021 02:37:52",
"store_id": 78929,
"external_warehouse_id": "60921610323",
"warehouse_id": 54534,
"add_qty_value": "0%",
"add_incoming_qty_value": "0%",
"tenant_id": 60895
}
],
"old_shopify_locations": [
{
"id": "60921610323",
"name": "Supplier",
"warehouseId": 54534,
"warehouseName": "Lux"
},
{
"id": "34639577171",
"name": "Bedfordview Warehouse",
"warehouseId": 54533,
"warehouseName": "Bedfordview Warehouse"
}
]
}
I have added following JSON Extractor to fetch store_config value from the above API response.
So as per above example, I am getting following store_config value via JSON Extractor.
{
"isOrderUpdateProcessor": false,
"auto_archive": true,
"tagOrderRisks": true,
"error_emails": "kyle#gmail.com",
"autoMapDuplicateProduct": true,
"enabledMultiCurrency": false,
"disableSyncPIIData": false
}
So Now, I want to fetch "isOrderUpdateProcessor" value from the above store_config variable.
So please help me into this.
You can use another JSON Extractor to extract from your variable by choosing the radio button JMeter variable and add your expression as$.isOrderUpdateProcessor and save result in a new variable
If you want to extract isOrderUpdateProcessor from the result
Add a JSON Extractor just below you JSON extractor
Configure the JSON Extractor to extract the value isOrderUpdateProcessor from the JMeter variable store_config
Given you have groovy in your tags it would be something like:
vars.put('foo', new groovy.json.JsonSlurper().parseText(vars.get('StoreConfig')).isOrderUpdateProcessor as String)
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Parse JSON message in Logstash

I am sending my jenkins logs to logstash with following config:
redis {
host => "localhost"
key => "logstash"
data_type => "list"
codec => json
}
This works as smooth as expected, now i see follwoing message in KIBANA:
{
"_index": "logstash-2015.12.18",
"_type": "logs",
"_id": "AVG1BN5LXZBIbp7HE4xN",
"_score": null,
"_source": {
"data": {
"id": "965",
"projectName": "NicePJ",
"displayName": "#965",
"fullDisplayName": "NicePJ",
"url": "job/NIcePJ/965/",
"buildHost": "Jenkins",
"buildLabel": "master",
"buildNum": 965,
"buildDuration": 1,
"rootProjectName": "NicePJ",
"rootProjectDisplayName": "#965",
"rootBuildNum": 965,
"buildVariables": {
"target_SUT": "0201",
"report_warnings": "false",
"product": "Ours",
"testsuite": "Exciting_stuff5",
"qft_version": "current",
"target_task": "t324",
"branch": "test",
"testcase": "",
"revision": "HEAD",
"node": "hsqs960",
"client": "Desktop",
"run_specific_test": "false",
"user": "xxxxx"
}
},
"message": [
"A This is a message XYZ"
],
"source": "jenkins",
"source_host": "http://serverXL:8080/",
"#timestamp": "2015-12-18T12:16:02.000Z",
"#version": 1
},
"fields": {
"#timestamp": [
1450440962000
]
},
"sort": [
1450440962000
]
}
Now i want to filter the message field for certain messages, but i cant get it work. How can i filter the message field and how can i access the buildHost field to use it in an if statement in the pipeline?
Following i tried after many examples:
if[data][buildHost]== "jenkins"
{
grok
{
match => { "message[0]" => "\[exec\]\s*\<%{GREEDYDATA:test}\s*\[%{GREEDYDATA:result}\]" }
}
}
But this is just not working at all, please help me out.
Conditional
The == compares simple string and case sensitive, so "jenkins" will not match as your data shows ("buildHost": "Jenkins",):
if[data][buildHost]== "jenkins"
But following does:
if[data][buildHost]== "Jenkins"
If you need match both, you can either use || or regex =~.
Grok
The grok is a filter to parse message with regex pattern. You can test your regex pattern with
online grok debugger
Kibana dev tools's grok debugger

Liferay API update Data with JSON

I am working on an API for my Portal, to provide users the ability to update there data via API directly from there internal Systems.
Liferay 6.2 bundle Tomcat. (for api call testing I use soapUI)
The get Methods work fine, (I have getAll, and getByID). getByID returns a JSON Object like this:
{
"ID": "ORGANIZATIONID",
"type": "ORGANIZATIONTYPE",
"name": "ORGANIZATIONNAME",
"URI": "ORGANIZATIONNAMEURI"
"date of inclusion": "INCLUTIONDATE",
"last activities": "LASTMODIFIEDDATE",
"address": {
"name of host institution": "NAMEOFHOSTINSTITUTE",
"street1": "STREET1",
"street2" : "STREET2",
"zip": "ZIP",
"city": "CITY",
"country": "COUNTRY",
},
"url": [{"ORGANIZATIONURL"}],
"main contact": {
"first name": "FIRSTNAME",
"last name" : "LASTNAME",
"phone": "PHONE",
"email": "MAINCONTACTEMAIL"
},
"type of host institution" : "TYPEOFHOSTINSTITUTE",
"diseases": [
{
"name": "DISEASENAME1",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
},
{
"name": "DISEASENAME2",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
}
]
}
I would like to have an API for Updating the diseases information for an organization. I have created a URL service where everything is in the url as parameters, but I would like to have it that the in the url only the id parameter is send and the rest of the information in a JSON object. Like:
/api/jsonws/....-portlet...../updateregbb/organization-id/16016/
and the data:
{
"ID": "ORGANIZATIONID",
"diseases": [
{
"name": "DISEASENAME1",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
},
{
"name": "DISEASENAME2",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
}
]
}
But I could not find a solution how I can read the JSON data:
#JSONWebService(value = "updateregbb", method = "POST")
public void updateregbb2(long organizationId, Map jsondata) {
#JSONWebService(value = "updateregbb", method = "POST")
public void updateregbb(long organizationId, JSONObject json) {
How can I get the Data into my function?
Thanks for the help,
Best
Robert

Backbone.js Collections

I am working with backbone.js. I am trying to send a request to restful service i am getting the resultset as json object as shown
{
"Msgs": [
"Alert",
"Not"
],
"MessageStatus": [
"Active",
"Inactive"
],
"date": {
"From": "2013-04-25",
"To": "2013-06-25"
},
"Mlist": {
"Status": "PND",
"Role": "Admin,User",
"To": "2013-06-24",
"Id": 6,
"Datecreated": "2013-06-24",
"Title": "Title5",
"From": "2013-06-20"
}
}.
I am putting the json object extracting and setting it to collection but I am not able to get particular model from the collection with specific id.
If you want to make your model can be identified with id. You have to set the id in the attributes hash:
{
"id": 1001,
"Msgs": [
"Alert",
"Not"
],
"MessageStatus": [
"Active",
"Inactive"
],
"date": {
"From": "2013-04-25",
"To": "2013-06-25"
},
"Mlist": {
"Status": "PND",
"Role": "Admin,User",
"To": "2013-06-24",
"Id": 6,
"Datecreated": "2013-06-24",
"Title": "Title5",
"From": "2013-06-20"
}
}
Then using backbone collection "findWhere" method to get the specific id model.
//assume msgCollection is which you put the models
var model = msgCollection.findWhere({id: 1001});
Hope this is helpful for you.
I think you need to set your model and parse your input data. Checkout http://backbonejs.org/#Collection-model, http://backbonejs.org/#Model-parse and http://backbonejs.org/#Collection-parse.