Linux JQ. How to extract data from specific ID Entry - json

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

Related

Problem in getting json key value in nested json using jq

Objective: Trying to get a key's value from a json file using jq function. Input source is a file and need to fetch specific key value to use it in further process of the flow
Input Json:
[
{
"accessTier": "Hot",
"allowBlobPublicAccess": false,
"allowCrossTenantReplication": null,
"enableNfsV3": false,
"encryption": {
"encryptionIdentity": null,
"keySource": "Microsoft.Storage",
"keyVaultProperties": null,
"requireInfrastructureEncryption": null,
"services": {
"blob": {
"enabled": true,
"keyType": "Account",
"lastEnabledTime": "xxxxxx"
},
"file": {
"enabled": true,
"keyType": "Account",
"lastEnabledTime": "xxxxxx"
},
"queue": null,
"table": null
}
},
"extendedLocation": null,
"failoverInProgress": null,
"geoReplicationStats": null,
"id": "/subscriptions/xxxx-xxxx-xxxxx-xxxx/resourceGroups/xxxxxxxxxxxxe/providers/Microsoft.Storage/storageAccounts/xxxxxxxxxxxx",
"identity": {
"principalId": null,
"tenantId": null,
"type": "None",
"userAssignedIdentities": null
},
"immutableStorageWithVersioning": null,
"isHnsEnabled": true,
"isLocalUserEnabled": null,
"isSftpEnabled": null,
"keyCreationTime": {
"key1": "xxxxxxx",
"key2": "xxxxxxx"
},
"keyPolicy": null,
"kind": "StorageV2",
"largeFileSharesState": null,
"lastGeoFailoverTime": null,
"location": "xxxxxxxx",
"minimumTlsVersion": "TLS1_0",
"name": "storageaccountfortest",
"networkRuleSet": {
"bypass": "xxxxxxx",
"defaultAction": "Allow",
"ipRules": [],
"resourceAccessRules": null,
"virtualNetworkRules": []
},
"primaryLocation": "xxxxxxxxx",
"privateEndpointConnections": [],
"provisioningState": "Succeeded",
"publicNetworkAccess": null,
"resourceGroup": "xxxxxxxxx",
"routingPreference": null,
"sasPolicy": null,
"secondaryEndpoints": null,
"secondaryLocation": null,
"sku": {
"name": "Standard_LRS",
"tier": "Standard"
}
}
]
What I tried:
user#ablab:~$ jq '.name' input.json
jq: error (at input.json:100): Cannot index array with string "name"
user#ablab:~$
How to get key name value from above mentioned json. There is no deeper nested subsection of keys where I need to search. Please help to find how to fix this issue
If you don't want to be bothered with having to figure out the path to the key of interest, and if you don't mind the possibility that there might be several occurrences of a particular key name, then the following one-liner may be worth considering:
.. | objects | select(.name).name
I solved issue with below
user#ablab:~$ jq -r '.[] | .name' input.json

VBA JSON Parsing with VBA-Web Assistance

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")

How to filter JSON array using JQ in shell (STRIPE)?

Hello internet people,
Apparently I had the same problem of many but after searching a lot even here I could not find a reasonable answer at all!
My problem:
On shell I did enter the command:
stripe invoices list --customer MY_CUSTOMER_ID_HERE
Then the following JSON appeared:
{
"object": "list",
"data": [
{
"id": "INVOICE_ID",
"object": "invoice",
"account_country": "COUNTRY",
"account_name": "MY_BUSYNESS_NAME",
"account_tax_ids": null,
"amount_due": 1000,
"amount_paid": 1000,
"amount_remaining": 0,
"application_fee_amount": null,
"attempt_count": 1,
"attempted": true,
"auto_advance": false,
"billing_reason": "subscription_create",
"charge": "CHARGE_ID",
"collection_method": "charge_automatically",
"created": SOME_UNIXTIME,
"currency": "usd",
"custom_fields": null,
"customer": "CUSTOMER_ID",
"customer_address": null,
"customer_email": "CUSTOMER_EMAIL",
"customer_name": null,
"customer_phone": "CUSTOMER_PHONE",
"customer_shipping": null,
"customer_tax_exempt": "none",
"customer_tax_ids": [
],
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [
],
"description": null,
"discount": null,
"discounts": [
],
"due_date": null,
"ending_balance": 0,
"footer": null,
"hosted_invoice_url": "INVOICE_URL_GOES_HERE",
"invoice_pdf": "INVOICE_PDF_URL_GOES_HERE",
"last_finalization_error": null,
"lines": {
"object": "list",
"data": [
{
"id": "ID",
"object": "line_item",
"amount": 1000,
"currency": "usd",
"description": "PLAN_DESCRIPTION",
"discount_amounts": [
],
"discountable": true,
"discounts": [
],
"livemode": false,
"metadata": {
},
"period": {
"end": SOME_UNIXTIME,
"start": SOME_UNIXTIME
},
"plan": {
"id": "PRICE_ID_HERE",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": SOME_UNIXTIME,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"nickname": null,
"product": "PRODUCT_ID_HERE",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "MORE_ID_HERE",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": SOME_UNIXTIME,
"currency": "usd",
"livemode": false,
"lookup_key": null,
"metadata": {
},
"nickname": null,
"product": "PRODUCT_ID_HERE",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 1000,
"unit_amount_decimal": "1000"
},
"proration": false,
"quantity": 1,
"subscription": "SUBSCRIPTION_ID_HERE",
"subscription_item": "SUBS_ITEM_ID_HERE",
"tax_amounts": [
],
"tax_rates": [
],
"type": "subscription"
}
],
"has_more": false,
"total_count": 1,
"url": "IV_URL_HERE"
},
"livemode": false,
"metadata": {
},
"next_payment_attempt": null,
"number": "NUMBER_IV_XXXX",
"on_behalf_of": null,
"paid": true,
"payment_intent": "PAYMENT_INTENT_ID_HERE",
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null
},
"period_end": SOME_UNIXTIME,
"period_start": SOME_UNIXTIME,
"post_payment_credit_notes_amount": 0,
"pre_payment_credit_notes_amount": 0,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"status": "paid",
"status_transitions": {
"finalized_at": SOME_UNIXTIME,
"marked_uncollectible_at": null,
"paid_at": SOME_UNIXTIME,
"voided_at": null
},
"subscription": "SUBSCRIPTION_ID_HERE",
"subtotal": 1000,
"tax": null,
"total": 1000,
"total_discount_amounts": [
],
"total_tax_amounts": [
],
"transfer_data": null,
"webhooks_delivered_at": SOME_UNIXTIME
}
],
"has_more": false,
"url": "/some_path"
}
I'm able to access the second level with this command:
stripe invoices list --customer MY_CUSTOMER_ID_HERE | [.id,.attempt_count,.billing_reason,.created,.customer,.customer_email,.lines.data[],.paid,.status]'
Which returns a nice response, but I do want to filter few values inside data (data.lines.data[]) like id, plan and inside plan the id, currency and product for example.
My question:
Does any one knows how to do that with this kind of commands?
Just in case, those are the questions and answer that I tried but couldn't figured how to do it at all!
How to filter array of objects by element property values using jq?
How to filter arrays in json based on specific value using jq
How to filter nested arrays in JSON while maintaining structure using jq
filtering from JSON output from curl using JQ
Filtering JSON by object name using jq
How to filter JSON using jq stream (duplicate)
How to filter and replace values in json with jq
Filtering JSON list in shell using jq
I did also try a lot attempts on my own, like:
stripe invoices list --customer MY_CUSTOMER_ID_HERE | [.id,.attempt_count,.billing_reason,.created,.customer,.customer_email,.lines.data[.id.plan[][.id,.plan[.id,.currency,.product]],.paid,.status]'
Response:
jq: error (at :179): Cannot index string with string "plan"
stripe invoices list --customer MY_CUSTOMER_ID_HERE | [.id,.attempt_count,.billing_reason,.created,.customer,.customer_email,.lines.data[][],.paid,.status]'
This one return a null value for the second data content.
Response:
[
"DESIRED_ID",
1,
"value_retrieved_ok",
unixtime_ok,
"SOME_ID_OK",
"customer_mail#ok.com",
null, <<< ???
true,
"paid"
]
EDIT:
Desired result should look like:
[
"DESIRED_ID",
1,
"value_retrieved_ok",
unixtime_ok,
"SOME_ID_OK",
"customer_mail#ok.com",
"id" // the id inside lines.data{}
["id", // the id inside lines.data.plan
"currency", // the currency inside lines.data.plan
"product" // the product inside lines.data.plan
]
true,
"paid"
]
Anyway I do hope that there's a good soul out there have mercy!
Your requirements are a bit difficult to follow, especially since the "expected output" does not correspond exactly to the given input, but the following is either what you're looking for, or very close to it:
.data[]
| [.id,.attempt_count,.billing_reason,.created,.customer,.customer_email]
+ (.lines.data[] | [.id, [.plan.id, .plan.currency, .plan.product]])
+ [.paid,.status]

Transform a Map to List

I am trying to transform the map of the object in json to List using JQ ...
Source:
{
"title": "title",
"keyword": "keyword",
"desc": {
"user1": {
"name": "kumar",
"Duration": null,
"Time": null,
" Dominance": "Upper Field First"
},
"user2": {
"id": "user2",
"name": "user2",
"Duration": null,
"Time": null,
" Dominance": "Upper Field First"
}
}
}
Target:
[
{
"id": "user1",
"name": "kumar",
"Duration": null,
"Time": null,
"Dominance": "Upper Field First"
},
{
"id": "user2",
"name": "user2",
"Duration": null,
"Time": null,
"Dominance": "Upper Field First"
}
]
I tried various options like , but not able to get the extact thing i need.
.desc | . as $in| keys[]
to_entries
JQ Workspace ::: https://jqplay.org/s/MiJ9w1Sz5L
The following filter will do what you want, assuming you've fixed the input:
.desc
| to_entries
| map( {id: .key} + .value)
To understand this, simply read up on to_entries in the online manual (see jq).

JQ - Cannot extract values from Ubibot json

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