Using Curl and PUT request to edit JSON file - json

I'm trying to edit an endpoint on a REST API that gives me an array of objects. I want to edit the json file but I've been having trouble with formatting the HTTP request.
the output of the endpoint is something like
"result" : [
{
"MAC" : "00:08:00:4A:A1:B3",
"available" : true,
"bridge" : "br0",
"ipv4" : {
"dns1" : "",
"dns2" : "",
"gateway" : "",
"ip" : "",
"mask" : "",
"mode" : ""
},
"ipv6" : {
"delegatedPrefixLength" : 64,
"dns1" : "",
"dns2" : "",
"enabled" : false,
"fixedIp" : [],
"gateway" : "",
"ip" : [],
"linkLocalIp" : [],
"mode" : "DELEGATED",
"prefixDelegationEnabled" : false
},
"name" : "eth0",
"nitype" : "ETHER",
"type" : "LAN"
},
{
"available" : false,
"bridge" : "br0",
"ipv4" : {
"dns1" : "",
"dns2" : "",
"gateway" : "",
"ip" : "",
"mask" : "",
"mode" : ""
},
"ipv6" : {
"delegatedPrefixLength" : 64,
"dns1" : "",
"dns2" : "",
"enabled" : false,
"fixedIp" : [],
"gateway" : "",
"ip" : [],
"linkLocalIp" : [],
"mode" : "DELEGATED",
"prefixDelegationEnabled" : false
},
"name" : "eth1",
"nitype" : "ETHER",
"type" : "LAN"
}
]
I need to be able to append some fields in the first object in the array. I have tried
curl -k -X PUT -H "Content-Type: application/json" -d '{[{"ipv4":{"mode":"DHCP"},"name": "eth0", "type":WAN}]"}' https://192.168.2.1/api/ni?token=$token1
but I keep getting an error saying that it's expecting a and object/value/array.
Any suggestions?

It looks like your PUT data does not have a top-level JSON field:
{[{"ipv4":{"mode":"DHCP"}, ...
The API is probably expecting something like this:
{"request": [{"ipv4":{"mode":"DHCP"}, ...
Check your documentation & examples for that API.

Related

Jmeter: Extracting JSON response with special/spaces characters

Hello can someone help me extract the value of user parameter which is "testuser1"
I tried to use this JSON Path expression $..data I was able to extract the entire response but unable to extract user parameter. Thanks in advance
{
"data": "{ "took" : 13, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "bushidodb_history_network_eval_ea9656ef-0a9b-474b-8026-2f83e2eb9df1_2021-april-10", "_type" : "network", "_id" : "6e2e58be-0ccf-3fb4-8239-1d4f2af322e21618059082000", "_score" : 1.0, "_source" : { "misMatches" : [ "protocol", "state", "command" ], "instance" : "e3032804-4b6d-3735-ac22-c827950395b4|0.0.0.0|10.179.155.155|53|UDP", "protocol" : "UDP", "localAddress" : "0.0.0.0", "localPort" : "12345", "foreignAddress" : "10.179.155.155", "foreignPort" : "53", "command" : "ping yahoo.com ", "user" : "testuser1", "pid" : "10060", "state" : "OUTGOINGFQ", "rate" : 216.0, "originalLocalAddress" : "192.168.100.229", "exe" : "/bin/ping", "md5" : "f9ad63ce8592af407a7be43b7d5de075", "dir" : "", "agentId" : "abcd-dcd123", "year" : "2021", "month" : "APRIL", "day" : "10", "hour" : "12", "time" : "1618059082000", "isMerged" : false, "timestamp" : "Apr 10, 2021 12:51:22 PM", "metricKey" : "6e2e58be-0ccf-3fb4-8239-1d4f2af322e2", "isCompliant" : false }, "sort" : [ 1618059082000 ] } ] }, "aggregations" : { "count_over_time" : { "buckets" : [ { "key_as_string" : "2021-04-10T08:00:00.000-0400", "key" : 1618056000000, "doc_count" : 1 } ] } }}",
"success": true,
"message": {
"code": "S",
"message": "Get Eval results Count Success"
}
}
Actual Response:
Images
What you posted doesn't look like a valid JSON to me.
If in reality you're getting what's in your image, to wit:
{
"data": "{ \"took\" : 13, \"timed_out\" : false, \"_shards\" : { \"total\" : 5, \"successful\" : 5, \"skipped\" : 0, \"failed\" : 0 }, \"hits\" : { \"total\" : 1, \"max_score\" : 1.0, \"hits\" : [ { \"_index\" : \"bushidodb_history_network_eval_ea9656ef-0a9b-474b-8026-2f83e2eb9df1_2021-april-10\", \"_type\" : \"network\", \"_id\" : \"6e2e58be-0ccf-3fb4-8239-1d4f2af322e21618059082000\", \"_score\" : 1.0, \"_source\" : { \"misMatches\" : [ \"protocol\", \"state\", \"command\" ], \"instance\" : \"e3032804-4b6d-3735-ac22-c827950395b4|0.0.0.0|10.179.155.155|53|UDP\", \"protocol\" : \"UDP\", \"localAddress\" : \"0.0.0.0\", \"localPort\" : \"12345\", \"foreignAddress\" : \"10.179.155.155\", \"foreignPort\" : \"53\", \"command\" : \"pingyahoo.com\", \"user\" : \"testuser1\", \"pid\" : \"10060\", \"state\" : \"OUTGOINGFQ\", \"rate\" : 216.0, \"originalLocalAddress\" : \"192.168.100.229\", \"exe\" : \"/bin/ping\", \"md5\" : \"f9ad63ce8592af407a7be43b7d5de075\", \"dir\" : \"\", \"agentId\" : \"abcd-dcd123\", \"year\" : \"2021\", \"month\" : \"APRIL\", \"day\" : \"10\", \"hour\" : \"12\", \"time\" : \"1618059082000\", \"isMerged\" : false, \"timestamp\" : \"Apr10, 202112: 51: 22PM\", \"metricKey\" : \"6e2e58be-0ccf-3fb4-8239-1d4f2af322e2\", \"isCompliant\" : false }, \"sort\" : [ 1618059082000 ] } ] }, \"aggregations\" : { \"count_over_time\" : { \"buckets\" : [ { \"key_as_string\" : \"2021-04-10T08: 00: 00.000-0400\", \"key\" : 1618056000000, \"doc_count\" : 1 } ] } }}",
"success": true,
"message": {
"code": "S",
"message": "Get Eval results Count Success"
}
}
the easiest way is just using 2 JSON Extractors:
Extract data attribute value into a JMeter Variable from the response
Extract user attribute value into a JMeter variable from ${data} JMeter Variable:
Demo:
If the response looks like exactly you posted you won't be able to use JSON Extractors and will have to treat it as normal text so your choice is limited to Regular Expression Extractor, example regular expression:
"user"\s*:\s*"(\w+)"
Add Regular Expression extractor with the corresponding request and extract it. Use the below expression.
Expression: "user" : "(.*?)"
Ref: https://jmeter.apache.org/usermanual/regular_expressions.html
Regular Expression Extractor Sample

Keycloak: is it possible to script groups in config json file?

I can configure keycloak user in my users.json file:
{
"realm" : "my_realm",
"users" : [ {
"id" : "my_id",
"createdTimestamp" : 1568195319743,
"username" : "my_user_name",
"enabled" : true,
"totp" : false,
"emailVerified" : false,
"credentials" : [ {
"type" : "password",
"hashedSaltedValue" : "XXXXX",
"salt" : "XXXXX",
"hashIterations" : 27500,
"counter" : 0,
"algorithm" : "pbkdf2-sha256",
"digits" : 0,
"period" : 0,
"createdDate" : 1568195377551,
"config" : { }
} ],
"disableableCredentialTypes" : [ "password" ],
"requiredActions" : [ ],
"realmRoles" : [ "offline_access", "uma_authorization" ],
"clientRoles" : {
"account" : [ "manage-account", "view-profile" ]
},
"notBefore" : 0,
"groups" : [ ]
}]
}
When I start the keycloak container, those get loaded into keycloak database.
Can I script user groups the same way (in this file or some other file)?
And map the users to the groups?
Turned out it's simple (but also not documented):
{
"realm" : "my_realm",
......................
"groups": [
{
"id": "<my group id>",
"name": "<my_group_name>",
"path": "/<my_group_name>",
"attributes": {},
"realmRoles": [],
"clientRoles": {},
"subGroups": []
}
]
}
}
I figured this out by creating groups in keycloak admin interface, clicking "Export" and reading the downloaded file.

Extract JSON value using Jmeter

I have this JSON:
{
"totalMemory" : 12206567424,
"totalProcessors" : 4,
"version" : "0.4.1",
"agent" : {
"reconnectRetrySec" : 5,
"agentName" : "1001",
"checkRecovery" : false,
"backPressure" : 10000,
"throttler" : 100
},
"logPath" : "/eq/equalum/eqagent-0.4.1.0-SNAPSHOT/logs",
"startTime" : 1494837249902,
"status" : {
"current" : "active",
"currentMessage" : null,
"previous" : "pending",
"previousMessage" : "Recovery:Starting pipelines"
},
"autoStart" : false,
"recovery" : {
"agentName" : "1001",
"partitionInfo" : { },
"topicToInitialCapturePosition" : { }
},
"sources" : [ {
"dataSource" : "oracle",
"name" : "oracle_source",
"captureType" : "directOverApi",
"streams" : [ ],
"idlePollingFreqMs" : 100,
"status" : {
"current" : "active",
"currentMessage" : null,
"previous" : "pending",
"previousMessage" : "Trying to init storage"
},
"host" : "192.168.191.5",
"metricsType" : { },
"bulkSize" : 10000,
"user" : "STACK",
"password" : "********",
"port" : 1521,
"service" : "equalum",
"heartbeatPeriodInMillis" : 1000,
"lagObjective" : 1,
"dataSource" : "oracle"
} ],
"upTime" : "157 min, 0 sec",
"build" : "0-SNAPSHOT",
"target" : {
"targetType" : "equalum",
"agentID" : 1001,
"engineServers" : "192.168.56.100:9000",
"kafkaOptions" : null,
"eventsServers" : "192.168.56.100:9999",
"jaasConfigurationPath" : null,
"securityProtocol" : "PLAINTEXT",
"stateMonitorTopic" : "_state_change",
"targetType" : "equalum",
"status" : {
"current" : "active",
"currentMessage" : null,
"previous" : "pending",
"previousMessage" : "Recovery:Starting pipelines"
},
"serializationFormat" : "avroBinary"
}
}
I trying using Jmeter to extract out the value of agentID, how can I do that using Jmeter, what would be better ? using extractor or json extractor?
what I am trying to do is to extract agentID value in order to use it on another http request sample, but first I have to extract it from this request.
thanks!
I believe using JSON Extractor is the best way to get this agentID value, the relevant JsonPath query will be as simple as $..agentID
Demo:
See the following reference material:
JsonPath - Getting Started - for initial information regarding JsonPath language, functions, operators, etc.
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios - for more complex scenarios.

Accessing to the users uid in Firebase

I have this JSON structure:
{
"Devices" : {
"1EJ8QmEQBJfBez7PMADbftCjVff1" : {
"Device1" : {
"Category" : "Others",
"Description" : "",
"DeviceName" : "",
"ImageUrl" : ""
},
"Device2" : {
"Category" : "Chairs",
"Description" : "",
"DeviceName" : "",
"ImageUrl" : ""
},
"Device3" : {
"Category" : "Others",
"Description" : "",
"DeviceName" : "",
"ImageUrl" : ""
}
},
"97PUAcUC5UYxLpBOLnC4yQjxiEf2" : {
"Device1" : {
"Category" : "Others",
"Description" : "",
"DeviceName" : "",
"ImageUrl" : ""
},
"Device2" : {
"Category" : "Books",
"Description" : "",
"DeviceName" : "",
"ImageUrl" : ""
},
"Device3" : {
"Category" : "Chairs",
"Description" : "",
"DeviceName" : "",
"ImageUrl" : ""
}
}
},
"UserProfile" : {
"1EJ8QmEQBJfBez7PMADbftCjVff1" : {
"city" : "",
"email" : "",
"name" : "",
"phone" : ""
},
"97PUAcUC5UYxLpBOLnC4yQjxiEf2" : {
"city" : "",
"email" : "",
"name" : "",
"phone" : ""
},
}
}
I want to access to the Category node in each device?
Is that possible?
Because I want to retrieve data for all nodes that have the same category!
let ref = FIRDatabase.database().reference().child("Devices")
ref.queryOrderedByChild("Category")
I have tried to access to devices! then direct to the category!
It seems like this is not the correct way!
Don't tell me that I need to change the structure of the JSON!
Is there any way to access to the user ids'?
In NoSQL databases you will often have to model your data for the use-cases that you app needs. With your current data structure you can efficiently load a user's devices.
To also efficiently allow to load the devices for a category, you will have to store that information too. Here's one way to do that:
"Categories": {
"Others": {
"1EJ8QmEQBJfBez7PMADbftCjVff1_Device1": "1EJ8QmEQBJfBez7PMADbftCjVff1/Device1",
"1EJ8QmEQBJfBez7PMADbftCjVff1_Device3": "1EJ8QmEQBJfBez7PMADbftCjVff1/Device3",
"97PUAcUC5UYxLpBOLnC4yQjxiEf2_Device1": "97PUAcUC5UYxLpBOLnC4yQjxiEf2/Device1"
},
"Chairs": {
"1EJ8QmEQBJfBez7PMADbftCjVff1_Device2": "1EJ8QmEQBJfBez7PMADbftCjVff1/Device2",
"97PUAcUC5UYxLpBOLnC4yQjxiEf2_Device3": "97PUAcUC5UYxLpBOLnC4yQjxiEf2/Device3"
},
"Books": {
"97PUAcUC5UYxLpBOLnC4yQjxiEf2_Device2": "97PUAcUC5UYxLpBOLnC4yQjxiEf2/Device2"
}
}
So the above structure uses <uid>_<devicekey> as the keys and then has the path to that item under /devices as its value.
I recommend reading this great article on NoSQL data modeling.

Push data in json array MongoDB

I have the following structure in my mondoDB:
"source" : [
{
"source_id" : "800000021",
"source" : "Test1"
}
]
"state" : "AM",
"owner_phone" : "0",
"latitude" : 0,
"leedon_activated" : "True",
"zip_code" : "12345",
"fax" : "0",
I want to add another json to the source array for the following resut:
"source" : [
{
"source_id" : "800000021",
"source" : "Test1"
},
{
"source_id" : "800000022",
"source" : "Test2"
}
]
"state" : "AM",
"owner_phone" : "0",
"latitude" : 0,
"leedon_activated" : "True",
"zip_code" : "12345",
"fax" : "0"
What will be MongoDB query for it?
Read about $push here
Your code will something like this
yourmodelname.update({ /* some conditions to updating files */ },{$push: {"source": {"source_id" : "213123", "source" : "asdasd"}}})