I have a Ubibot ws-1 and I'm trying to extract Timestamp, Temperature, Humidity, Light and voltage from last_values in the following JSON
The API JSON output is
{
"channel": {
"channel_id": "1111",
"name": "C-1111",
"field1": "Temperature",
"field2": "Humidity",
"field3": "Light",
"field4": "Voltage",
"field5": "WIFI RSSI",
"field6": "Vibration Index",
"field7": "Knocks",
"field8": "External Temperature Probe",
"field9": "Reed Sensor",
"field10": null,
"latitude": "1",
"longitude": "1",
"elevation": null,
"created_at": "2019-02-05T05:16:43Z",
"updated_at": "2019-02-05T06:11:38Z",
"metadata": "{\"fn_dp\":900,\"fn_th\":300,\"fn_light\":300,\"fn_acc_act\":0,\"thres_acc_min\":0,\"fn_acc_tap1\":0,\"fn_acc_tap2\":0,\"fn_ext_t\":300,\"fn_battery\":7200,\"fn_485_th\":0,\"fn_485_sth\":0,\"net_mode\":0,\"no_net_fn\":1,\"cg_data_led\":1,\"wifi_mode\":1}",
"tags": null,
"public_flag": "false",
"url": null,
"description": null,
"write_key": "1b0f2b31a6d522a376782a90f4be0270",
"user_id": "9186F150-9203-492B-B31A-77077E15E461",
"last_entry_id": "583",
"last_entry_date": "2019-02-05T19:12:47Z",
"usage": "96328",
"device_id": "17a867e84624895f6dab0346a4cba8cfd8279298",
"status": "{\"ssid\":\"hydra1\",\"status\":\"ssid=wifi,usb=1\",\"usb\":\"1\"}",
"status_date": "1549393967",
"last_ip": "nn.nn.nn.nnn",
"channel_icon": null,
"product_id": "ubibot-ws1",
"plan_code": "ubibot_free",
"plan_start": "2019-02-05T05:16:43Z",
"plan_end": null,
"bill_start": "2019-02-05T05:16:43Z",
"bill_end": "2019-03-07T05:16:43Z",
"traffic_out": "188763",
"traffic_in": "33767",
"full_dump": "0",
"renew": null,
"last_values": "{\"field1\":{\"value\":27.186996,\"created_at\":\"2019-02-05T19:12:39Z\"},\"field3\":{\"value\":10.37,\"created_at\":\"2019-02-05T19:12:39Z\"},\"field2\":{\"value\":29,\"created_at\":\"2019-02-05T19:12:39Z\"},\"field5\":{\"value\":-35,\"created_at\":\"2019-02-05T19:12:41Z\"},\"field4\":{\"value\":5,\"created_at\":\"2019-02-05T17:27:38Z\"}}",
}
}
my jq skills are very poor .channel.last_values."\(.field1) gives me jq: error (at <stdin>:0): Cannot index string with string "null".
Can someone help me with the correct jq? Thanks in advance
You need to parse last_values as JSON value first. To do that, use fromjson:
jq '.channel.last_values|fromjson.field1' file
Related
I have some JSON parsing in VBA that's driving me nuts. Here's a sample response:
{
"data": [
{
"type": "access_user",
"attributes": {
"name": "Me",
"email": null,
"phone": null,
"department": "",
"status": "current",
"source": null,
"guest_source": null,
"created_at": "2019-08-19T21:45:56Z",
"updated_at": "2019-08-22T03:38:33Z",
"pin": "77005",
"card_number": null
},
"id": "be66e054-5509-4cf6-a5c2-14b85eb25619",
"links": {
"self": "https://api"
}
},
{
"type": "access_user",
"attributes": {
"name": "Day Code",
"email": null,
"phone": null,
"department": null,
"status": "current",
"source": null,
"guest_source": null,
"created_at": "2019-08-21T19:49:29Z",
"updated_at": "2021-06-16T16:08:28Z",
"pin": "12345",
"card_number": null
},
"id": "3a615a3d-eb1c-4973-9e9f-ace5e29ca964",
"links": {
"self": "https://api"
}
}
],
"meta": {
"page": 1,
"per_page": 25,
"total_count": 2,
"total_pages": 1
}
}
Traversing "data" is fine, but when I traverse "attributes" I'm getting a "Type mismatch" error. Here's my code. I'm not seeing anything wrong. Any thoughts?
For Each Item In Response2.Data("data")
UserType = Item("type")
Id = Item("id")
If UserType = "access_user" Then
For Each Nested In Item("attributes")
Name = Nested("name")
status = Nested("status")
PIN = Nested("pin")
Next
End If
Next
Anyone's assistance is greatly appreciated!
The following two lines are equivalent . . .
For Each Nested In Item("attributes")
and
For Each Nested In Item("attributes").Keys()
So, in fact, you're looping through each key within the dictionary. And so your control variable Nested is assigned a string, hence the type mismatch.
Since Item("attributes") returns a dictionary object, you can eliminate the For Each/Next loop, and you can access your items as follows . . .
Name = Item("attributes")("name")
Status = Item("attributes")("status")
PIN = Item("attributes")("pin")
Guy, long term looking at this board and learning a lot but now stuck with little issue. Im working with Linux shell script that reads json (no problem here). What Im trying to do is get value from entry that has specific Type.
By parsing a json with just jq -r '.', I get
{
"records": [
{
"id": 01,
"type": "SOA",
"name": "#",
"data": "1800",
"priority": null,
"port": null,
"ttl": 1800,
"weight": null,
"flags": null,
"tag": null
},
{
"id": 02,
"type": "A",
"name": "#",
"data": "test.com",
"priority": null,
"port": null,
"ttl": 1800,
"weight": null,
"flags": null,
"tag": null
}
],
"links": {},
"meta": {
"total": 2
}
}
Then, I use "jq -r '.records' " and get:
[
{
"id": 01,
"type": "SOA",
"name": "#",
"data": "1800",
"priority": null,
"port": null,
"ttl": 1800,
"weight": null,
"flags": null,
"tag": null
},
{
"id": 02,
"type": "A",
"name": "#",
"data": "test.com",
"priority": null,
"port": null,
"ttl": 1800,
"weight": null,
"flags": null,
"tag": null
}
]
What I need to do is get data value of the type A. Currently, we have type SOA and A, but I need to only get data from A.
I can use dummy way of "jq -r '.records[1].data'" and it gives me correct response of test.com, but I want a more dynamic way of searching for specific type (in this case "A") and then giving the data value.
Thanks guys!
Is the field called domain_records or just records?
Use select to match your criteria
jq -r '.records[] | select(.type == "A").data'
test.com
Demo
I want to extract the key value generated from response, is there's a way to simulate using JSON Path extractor?
Sample Response:
{
"data": {
"1637042070532561": {
"symbol": "BAUa",
"side": "buy",
"quantity": "1",
"limitPrice": "2145",
"instrumentId": 4,
"created": "1637042070533",
"orderStatus": "rejected",
"type": "limit",
"executions": {
},
"decimals": 2,
"commission": "0",
"currency": "EUR",
"averagePrice": "0",
"id": "1637042070532561",
"filledStatus": "unfilled",
"filledPercent": "0.00",
"filledQty": "0"
}
},
"action": "set-orders",
"type": "orders",
"status": "OK",
"timestamp": "2021-11-16T05:54:30.536Z"
}
Expected Result: 1637042070532561
Go for JSON JMESPath Extractor, it provides keys function allowing querying JSON attribute names, in your case the query would be something like:
keys(data)
Demo:
More information: The JMeter JSON JMESPath Extractor and Assertion: A Guide
{
"success": true,
"data": [
{
"price_id": "3",
"product": "456",
"category": "53 Grade Cement",
"brand": "22",
"price": "290",
"price_unit": "bag",
"seller_id": null,
"status": "1",
"created_on": "2021-03-04 00:00:00",
"category_name": "Cement",
"brand_name": "Ramco"
}
]
}
I am guessing you are trying to request some data from server. So you've managed to request and get the data. Assign that data to a variable called response and then
you could just to this response.data[0].category.
I have a JSON object which has 100000 records. I want a select a record which has specific value to the one of the key
Eg:
[{
"name": "bindu",
"age": "24",
"qualification": "b.tech"
},
{
"name": "naveen",
"age": "23",
"qualification": "b.tech"
},
{
"name": "parvathi",
"age": "23",
"qualification": "m.tech"
},
{
"name": "bindu s",
"status": "married"
},
{
"name": "naveen k",
"status": "unmarried"
}]
now I want to combine the records which are having the name with 'bindu' and 'bindu s. We can achieve this by iterating on the JSON object but since the size is more it is taking more time. Is there any way to make this easy.
I want the output like
[{
"name": "bindu",
"age": "24",
"qualification": "b.tech",
"status": "married"
},
{
"name": "naveen",
"age": "23",
"qualification": "b.tech",
"status": "unmarried"
},
{
"name": "parvathi",
"age": "23",
"qualification": "m.tech"
"status": ""
},
This will rename and merge your objects by first name.
jq 'map(.name |= split(" ")[0]) | group_by(.name) | map(add)'