How to extract a specific value from JSON file? - json

I'm trying to extract a specific value from JSON file.
the key value is: "info": "this is an example" (The key is unique)
I want to extract only the value: "this is an example"
My code:
cat 9.json | jq '.info'
result:
null
JSON file example:
{
"Event": {
"id": "13",
"orgc_id": "1",
"org_id": "1",
"date": "2019-01-09",
"threat_level_id": "3",
"info": "test9",
"published": false,
"uuid": "5c35d180",
"attribute_count": "2",
"analysis": "0",
"timestamp": "1547044733",
"distribution": "1",
"proposal_email_lock": false,
"locked": false,
"publish_timestamp": "1547034089",
"sharing_group_id": "0",
"disable_correlation": false,
"extends_uuid": "",
"event_creator_email": "o#cyhgfnt.com",
"Org": {
"id": "1",
"name": "Cygfdgfdnt",
"uuid": "5b9f938d-e3a0-4ecb-83b3-0bdeac1b41bc"
},
"Orgc": {
"id": "1",
"name": "Cyhgfgft",
"uuid": "5b9f938d-e3a0-4ecb-83b3-0bdeac1b41bc"
},
"Attribute": [{
"id": "292630",
"type": "domain",
"category": "Network activity",
"to_ids": true,
"uuid": "5c35dd94-cccc-4086-b386-682823717aa5",
"event_id": "1357",
"distribution": "5",
"timestamp": "1547034584",
"comment": "This is a comment",
"sharing_group_id": "0",
"deleted": false,
"disable_correlation": false,
"object_id": "0",
"object_relation": null,
"value": "dodskj.com",
"Galaxy": [],
"ShadowAttribute": [],
"Tag": [{
"id": "223",
"name": "kill-chain:Exploitation",
"colour": "#a80079",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}]
}, {
"id": "292631",
"type": "ip-dst",
"category": "Network activity",
"to_ids": true,
"uuid": "5c35dd94-fe90-4ef6-b3a9-682823717aa5",
"event_id": "1357",
"distribution": "5",
"timestamp": "1547044733",
"comment": "comment example",
"sharing_group_id": "0",
"deleted": false,
"disable_correlation": false,
"object_id": "0",
"object_relation": null,
"value": "8.8.6.6",
"Galaxy": [],
"ShadowAttribute": [],
"Tag": [{
"id": "247",
"name": "maec-malware-capabilities:maec-malware-capability=\"anti-removal\"",
"colour": "#3f0004",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}, {
"id": "465",
"name": "osint:lifetime=\"perpetual\"",
"colour": "#006ebe",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}]
}],
"ShadowAttribute": [],
"RelatedEvent": [],
"Galaxy": [{
"id": "3",
"uuid": "698774c7-8022-42c4-917f-8d6e4f06ada3",
"name": "Threat Actor",
"type": "threat-actor",
"description": "Threat actors are characteristics of malicious actors (or adversaries) representing a cyber attack threat including presumed intent and historically observed behaviour.",
"version": "3",
"icon": "user-secret",
"namespace": "misp",
"GalaxyCluster": [{
"id": "6397",
"collection_uuid": "7cdff317-a673-4474-84ec-4f1754947823",
"type": "threat-actor",
"value": "Sofacy",
"tag_name": "misp-galaxy:threat-actor=\"Sofacy\"",
"description": "The Sofacy Group (also known as APT28, Pawn Storm, Fancy Bear and Sednit) is a cyber espionage group believed to have ties to the Russian government. Likely operating since 2007, the group is known to target government, military, and security organizations. It has been characterized as an advanced persistent threat.",
"galaxy_id": "3",
"source": "MISP Project",
"authors": ["Alexandre Dulaunoy", "Florian Roth", "Thomas Schreck", "Timo Steffens", "Various"],
"version": "82",
"uuid": "5b4ee3ea-eee3-4c8e-8323-85ae32658754",
"tag_id": "608",
"meta": {
"cfr-suspected-state-sponsor": ["Russian Federation"],
"cfr-suspected-victims": ["Georgia", "France", "Jordan", "United States", "Hungary", "World Anti-Doping Agency", "Armenia", "Tajikistan", "Japan", "NATO", "Ukraine", "Belgium", "Pakistan", "Asia Pacific Economic Cooperation", "International Association of Athletics Federations", "Turkey", "Mongolia", "OSCE", "United Kingdom", "Germany", "Poland", "European Commission", "Afghanistan", "Kazakhstan", "China"],
"cfr-target-category": ["Government", "Military"],
"cfr-type-of-incident": ["Espionage"],
"country": ["RU"],
"refs": ["https:\/\/en.wikipedia.org\/wiki\/Sofacy_Group", "https:\/\/aptnotes.malwareconfig.com\/web\/viewer.html?file=..\/APTnotes\/2014\/apt28.pdf", "http:\/\/www.trendmicro.com\/cloud-content\/us\/pdfs\/security-intelligence\/white-papers\/wp-operation-pawn-storm.pdf", "https:\/\/www2.fireeye.com\/rs\/848-DID-242\/images\/wp-mandiant-matryoshka-mining.pdf", "https:\/\/www.crowdstrike.com\/blog\/bears-midst-intrusion-democratic-national-committee\/", "http:\/\/researchcenter.paloaltonetworks.com\/2016\/06\/unit42-new-sofacy-attacks-against-us-government-agency\/", "https:\/\/www.cfr.org\/interactive\/cyber-operations\/apt-28", "https:\/\/blogs.microsoft.com\/on-the-issues\/2018\/08\/20\/we-are-taking-new-steps-against-broadening-threats-to-democracy\/", "https:\/\/www.bleepingcomputer.com\/news\/security\/microsoft-disrupts-apt28-hacking-campaign-aimed-at-us-midterm-elections\/", "https:\/\/www.bleepingcomputer.com\/news\/security\/apt28-uses-lojax-first-uefi-rootkit-seen-in-the-wild\/"],
"synonyms": ["APT 28", "APT28", "Pawn Storm", "PawnStorm", "Fancy Bear", "Sednit", "TsarTeam", "Tsar Team", "TG-4127", "Group-4127", "STRONTIUM", "TAG_0700", "Swallowtail", "IRON TWILIGHT", "Group 74"]
}
}]
}],
"Object": [],
"Tag": [{
"id": "608",
"name": "misp-galaxy:threat-actor=\"Sofacy\"",
"colour": "#12e000",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}, {
"id": "118",
"name": "gdpr:special-categories=\"health\"",
"colour": "#3ce600",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}]
}
}

I suppose you are trying to get the .info field inside .Event which should have been written as below. Use -r for without quotes
jq '.Event.info'

Related

Error parsing a specific JSON file in Snowflake with File Format

I have created a Stage and File Format in Snowflake which works with all my other JSON files except this, which throws an error:
Error parsing JSON: misplaced { File 'rooms.json.gz', line 1,
character 2 Row 0, column $1
I am using the same query that I am using for other files.
SELECT $1
FROM #MySchema.MY_STAGE/rooms.json.gz
;
What is wrong with the structure of this specific JSON file?
{
"rooms": [
{
"area": 131.49,
"longDescription": "",
"dateCreated": 1589908063390,
"reservable": false,
"name": "E249",
"remoteInfo": "",
"description": "",
"id": 2,
"type": {
"hexColor": "c16058",
"contentFlag": 1,
"cost": 0.0,
"dateCreated": 1308610520717,
"color": {},
"name": "BREAK ROOM",
"occupiable": false,
"id": 120,
"parkingSpace": false,
"dateUpdated": 1591818585913,
"typeCode": ""
},
"floor": {
"area": 25312.9878,
"dateCreated": 1589907703870,
"drawingAvailable": true,
"interiorGross": 0.0,
"name": "2",
"leaseArea": 0.0,
"id": 12,
"building": {
"address": {
"country": {
"defaultSelected": true,
"subdivisionCategoryName": "state",
"alpha2Code": "US",
"isoCode": "US",
"name": "United States of America (the)",
"id": 223
},
"city": "Some City",
"street": "Some Drive",
"postalCode": "00000",
"state": {
"country": {
"defaultSelected": true,
"subdivisionCategoryName": "state",
"alpha2Code": "US",
"isoCode": "US",
"name": "United States of America (the)",
"id": 223
},
"defaultSelected": false,
"code": "XX",
"name": "Some State",
"id": 66,
"categoryName": "state"
}
},
"code": "B2",
"dateCreated": 1589907508020,
"metric": false,
"name": "Some name",
"location": {},
"revitLink": "",
"id": 45,
"dateUpdated": 1601315841453,
"costCenters": []
},
"dateUpdated": 1600441936663
},
"capacity": 0,
"dateUpdated": 1600441936960
}
]
}
Edit: Screenshot from Notepad++ with all characters enabled

Filtering output to needed values in json

I have below output generated as part of gathering all attributes about openstack VM. I am trying to work a json filter, which would let me select just values of interest. (tenant name, tenant ID or cloud name and so on).
I am new to filters, so all the help is appreciated. I tried different ways but not close to what i want.
N_ID": true,
"NAME_ATTR": "name",
"OS-DCF:diskConfig": "AUTO",
"OS-EXT-AZ:availability_zone": "cloud-b",
"OS-EXT-STS:power_state": 1,
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "active",
"OS-SRV-USG:launched_at": "2018-06-05T22:45:50.000000",
"OS-SRV-USG:terminated_at": null,
"accessIPv4": "192.168.0.1",
"accessIPv6": "",
"addresses": {
"tenant-internal-direct-net": [
{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:73:c6:9e",
"OS-EXT-IPS:type": "fixed",
"addr": "192.168.0.1",
"version": 4
}
]
},
"az": "cloud-1-b",
"cloud": "cloud-1",
"config_drive": "",
"created": "2018-06-05T22:45:37Z",
"flavor": {
"id": "15",
"name": "2vCPUx8GB"
},
"hostId": "f12acb7a6a4050672a7710da4ccb5987916ac46648fc667496c4a169",
"human_id": "myhost-ost1",
"id": "ae12d6cd-c9b8-4908-a0f9-6af614f01197",
"image": {
"id": ""
},
"interface_ip": "192.168.0.1",
"key_name": "pz-rhel7",
"metadata": {},
"name": "myhost-ost1",
"networks": {
"tenant-internal-direct-net": [
"192.168.0.1"
]
},
"os-extended-volumes:volumes_attached": [
{
"id": "179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1"
}
],
"private_v4": "",
"progress": 0,
"public_v4": "192.168.0.1",
"public_v6": "",
"region": "cloud-1",
"request_ids": [],
"security_groups": [
{
"description": "",
"id": "55991c66-beb5-4750-b8f6-06025a1ce7d4",
"name": "SSHping",
"security_group_rules": [
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "53fa8fca-d54c-4462-9f5e-a748fed86885",
"port_range_max": null,
"port_range_min": null,
"protocol": "icmp",
"remote_ip_prefix": "0.0.0.0/0",
"security_group_id": "55991c66-beb5-4750-b8f6-06025a1ce7d4"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "841446f5-a7bf-46e9-91c2-f505c5842bc6",
"port_range_max": 22,
"port_range_min": 22,
"protocol": "tcp",
"remote_ip_prefix": "0.0.0.0/0",
"security_group_id": "55991c66-beb5-4750-b8f6-06025a1ce7d4"
}
]
},
{
"description": "Default security group",
"id": "987efb61-d7f3-47d5-9362-42f66d1b104b",
"name": "default",
"security_group_rules": [
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "79da1cc2-2128-4937-95ca-d0fc757bbfb3",
"port_range_max": 22,
"port_range_min": 22,
"protocol": "tcp",
"remote_ip_prefix": "0.0.0.0/0",
"security_group_id": "987efb61-d7f3-47d5-9362-42f66d1b104b"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "901d230e-9cd5-486c-aa89-c279b359fac9",
"port_range_max": 1,
"port_range_min": 1,
"protocol": "tcp",
"remote_ip_prefix": "0.0.0.0/0",
"security_group_id": "987efb61-d7f3-47d5-9362-42f66d1b104b"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "950c6b8c-e129-4138-8dd5-92254c5bd6c8",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_ip_prefix": null,
"security_group_id": "987efb61-d7f3-47d5-9362-42f66d1b104b"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "cf20d16d-0b8a-4bd7-b6b4-cab6d5968a44",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_ip_prefix": null,
"security_group_id": "987efb61-d7f3-47d5-9362-42f66d1b104b"
}
]
}
],
"status": "ACTIVE",
"tenant_id": "99f1f837a8004092926887d46188e726",
"updated": "2018-06-05T22:45:50Z",
"user_id": "35f6e9300037f9c6d23f08c5af8b324c4ab23688d2a146bd6aac601377ed72ef",
"volumes": [
{
"HUMAN_ID": false,
"NAME_ATTR": "name",
"attachments": [
{
"attached_at": "2018-06-05T22:45:43.000000",
"attachment_id": "4602ad6b-8652-4702-910b-e4e82a894a32",
"device": "/dev/vda",
"host_name": null,
"id": "179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1",
"server_id": "ae12d6cd-c9b8-4908-a0f9-6af614f01197",
"volume_id": "179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1"
}
],
"availability_zone": "nova",
"bootable": true,
"consistencygroup_id": null,
"created_at": "2018-06-05T22:45:39.000000",
"description": "",
"device": "/dev/vda",
"display_description": "",
"display_name": "",
"encrypted": false,
"human_id": null,
"id": "179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1",
"links": [
{
"href": "http://cloud-1.mycloud.com:8776/v2/99f1f837a8004092926887d46188e726/volumes/179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1",
"rel": "self"
},
{
"href": "http://cloud-1.mycloud.com:8776/99f1f837a8004092926887d46188e726/volumes/179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1",
"rel": "bookmark"
}
],
"metadata": {
"attached_mode": "rw",
"readonly": "False"
},
"multiattach": false,
"name": "",
"os-vol-tenant-attr:tenant_id": "99f1f837a8004092926887d46188e726",
"replication_status": "disabled",
"request_ids": [],
"size": 11,
"snapshot_id": null,
"source_volid": null,
"status": "in-use",
"updated_at": "2018-06-05T22:45:43.000000",
"user_id": "35f6e9300037f9c6d23f08c5af8b324c4ab23688d2a146bd6aac601377ed72ef",
"volume_image_metadata": {
"checksum": "2d3f40fa841b1c35c10e74207d1221f8",
"container_format": "bare",
"disk_format": "raw",
"hw_disk_bus": "scsi",
"hw_qemu_guest_agent": "yes",
"hw_scsi_model": "virtio-scsi",
"hw_vif_multiqueue_enabled": "true",
"hw_watchdog_action": "reset",
"image_id": "fe463cc5-fecf-4aad-a77e-70987f421169",
"image_name": "RHEL-7_4-MG",
"min_disk": "0",
"min_ram": "0",
"size": "10737418240"
},
"volume_type": "ceph-standard",
"x_openstack_request_ids": []
}
],
"x_openstack_request_ids": []
}

How to get a json key that contains a specific string with jq?

I have a json data like this:
{
"success": true,
"module": {
"endpoint": {
"mode": "pc",
"protocolVersion": "2.0"
},
"reload": true,
"data": {
"leftContainer_CL": {
"id": "CL",
"tag": "leftContainer",
"fields": {
"css": {
"floatPosition": "left",
"width": "788px"
},
"tag": "leftContainer"
},
"type": "container"
},
"container_C": {
"id": "C",
"tag": "container",
"fields": {
"css": {
"marginTop": "12px"
},
"tag": "container"
},
"type": "container"
},
"delivery_dfdaf8a8a": {
"id": "dfdaf8a8a",
"tag": "delivery",
"fields": {
"selectPos": "right",
"deliveryBy": {
"text": "Disediakan oleh",
"poster": "ALL ITEM STORE"
},
"options": [
{
"highlight": false,
"deliveryId": "STANDARD",
"bgColor": "#fafafa",
"price": "Rp18.900",
"disable": false,
"reachTime": "Dapatkan pada\n 3-4 Apr 2018",
"liveUp": false,
"selected": true
}
],
"style": "bar"
},
"type": "biz"
},
"rightContainer_CR": {
"id": "CR",
"tag": "rightContainer",
"fields": {
"css": {
"floatPosition": "right",
"width": "388px"
},
"tag": "rightContainer"
},
"type": "container"
},
"delivery_d43597338a": {
"id": "d43597338a",
"tag": "delivery",
"fields": {
"selectPos": "right",
"deliveryBy": {
"text": "Disediakan oleh",
"poster": "incredible accessories hp"
},
"options": [
{
"highlight": false,
"deliveryId": "STANDARD",
"bgColor": "#fafafa",
"price": "Rp37.800",
"disable": false,
"reachTime": "Dapatkan pada\n 3-4 Apr 2018",
"liveUp": false,
"selected": true
}
],
"style": "bar"
},
"type": "biz"
},
"orderSummary_6": {
"id": "6",
"tag": "orderSummary",
"fields": {
"isOpen": "false",
"summarys": [
{
"tail": "(3 barang)",
"title": "Subtotal",
"value": "Rp23.557"
},
{
"title": "Biaya pengiriman",
"value": "Rp56.700"
}
],
"title": "Ringkasan Pesanan\r\n"
},
"type": "biz"
},
"root_0": {
"id": "0",
"tag": "root",
"fields": {
"count": 3,
"title": "Troli belanja Saya"
},
"type": "root"
},
"item_i77997d6b": {
"id": "i77997d6b",
"tag": "item",
"fields": {
"img": "https://id-live.slatic.net/original/08c1396908dc240625751b09decb4211.jpg",
"quantity": {
"qtyPrefix": "Kuantitas",
"min": 1,
"autoOptions": false,
"quantity": 1,
"max": 5,
"editable": true,
"showIncrDecr": true,
"showOptions": false,
"step": 1
},
"sellerName": "ALL ITEM STORE",
"title": "Case Slim Black Matte Xiaomi Redmi 4A Softcase Black",
"stockTip": {},
"valid": true,
"itemId": "143800088",
"operations": [
"wishlist",
"delete"
],
"sellerId": "100124080",
"price": {
"price": 6000,
"currentPrice": "Rp6.000",
"originPrice": "Rp30.000",
"promotionRatio": "-80%"
},
"restriction": false,
"isGift": false,
"sku": {
"skuText": "Softcase, Hitam",
"productVariant": "SO908ELAAVYY4AANID-72544754",
"brandId": "17818",
"skuId": "157608391"
},
"itemUrl": "https://www.lazada.co.id/products/i143800088-s157608391.html?urlFlag=true&mp=1",
"cartItemId": 2006547819
},
"type": "biz"
},
"location_2": {
"id": "2",
"tag": "location",
"fields": {
"buttonText": "GANTI\r\n",
"editable": true,
"postCode": "",
"style": "casAddress",
"label": "Lokasi",
"title": "Jawa Tengah,Kab. Boyolali,Ampel",
"addressId": "R2388357-R80010396-R80015219"
},
"type": "biz"
},
"voucherInput_7": {
"id": "7",
"tag": "voucherInput",
"fields": {
"buttonText": "GUNAKAN",
"placeHolder": "Masukkan Kode Voucher",
"status": "default"
},
"type": "biz",
"validate": {
"value": [
{
"msg": "Maaf, voucher ini tidak dapat digunakan. Silahkan periksa jika ada kesalahan penulisan",
"regex": "^$|^[ ]{0,5}[A-Za-z0-9~!##%&*()_+?<>{}|-]{1,100}[ ]{0,5}$"
}
]
}
},
"shop_43597338a_s2c": {
"id": "43597338a_s2c",
"tag": "shop",
"fields": {
"badges": [],
"link": "//www.lazada.co.id/shop/incredible-accessories-hp",
"name": "incredible accessories hp"
},
"type": "biz"
},
"item_i7799f86e": {
"id": "i7799f86e",
"tag": "item",
"fields": {
"img": "http://id-live-02.slatic.net/p/2/case-anti-shock-anti-crack-elegant-softcase-for-xiaomi-redmi-5a-white-clear-free-tempered-glass-1273-94487227-9f8ddff53bde3f8de9eb514ba2172361-catalog.jpg",
"quantity": {
"qtyPrefix": "Kuantitas",
"min": 1,
"autoOptions": false,
"quantity": 1,
"max": 5,
"editable": true,
"showIncrDecr": true,
"showOptions": false,
"step": 1
},
"sellerName": "incredible accessories hp",
"title": "Case Anti Shock / Anti Crack Elegant Softcase for Xiaomi Redmi 5A - White Clear + Free Tempered Glass",
"stockTip": {},
"valid": true,
"itemId": "160714927",
"operations": [
"wishlist",
"delete"
],
"sellerId": "53631",
"price": {
"price": 13580,
"currentPrice": "Rp13.580",
"originPrice": "Rp25.000",
"promotionRatio": "-46%"
},
"restriction": false,
"isGift": false,
"sku": {
"skuText": "Softcase, Bening",
"productVariant": "SO908ELAB716EPANID-97510528",
"brandId": "17818",
"skuId": "183461134"
},
"itemUrl": "https://www.lazada.co.id/products/i160714927-s183461134.html?urlFlag=true&mp=1",
"cartItemId": 2006579310
},
"type": "biz"
},
"package_p43597338a": {
"id": "p43597338a",
"tag": "package",
"fields": {},
"type": "biz"
},
"listHeader_H": {
"id": "H",
"tag": "listHeader",
"fields": {
"middle": "HARGA",
"left": "3 barang",
"right": "KUANTITAS"
},
"type": "biz"
},
"delivery_3": {
"id": "3",
"tag": "delivery",
"fields": {
"selectPos": "left",
"options": [
{
"highlight": false,
"deliveryId": "STANDARD",
"bgColor": "#fafafa",
"price": "Rp56.700",
"disable": false,
"icon": "https://laz-img-cdn.alicdn.com/tfs/TB1UpyCpfDH8KJjy1XcXXcpdXXa-72-72.png",
"name": "Standar",
"reachTime": "Dapatkan pada\n 3-4 Apr 2018",
"liveUp": false,
"selected": true
}
],
"style": "card",
"title": "Pengiriman yang dipilih"
},
"type": "biz"
},
"orderTotal_8": {
"id": "8",
"tag": "orderTotal",
"fields": {
"button": {
"enable": true,
"text": "LANJUTKAN KE PEMBAYARAN",
"clicked": false
},
"payment": {
"taxTip": "Termasuk PPN, jika berlaku",
"pay": "Rp80.257",
"title": "Total"
}
},
"type": "biz"
},
"shop_fdaf8a8a_s23f9": {
"id": "fdaf8a8a_s23f9",
"tag": "shop",
"fields": {
"badges": [],
"link": "//www.lazada.co.id/shop/all-item-store",
"name": "ALL ITEM STORE"
},
"type": "biz"
},
"floatTips_4": {
"id": "4",
"tag": "floatTips",
"fields": {
"tips": []
},
"type": "biz"
},
"item_i7790e0f9": {
"id": "i7790e0f9",
"tag": "item",
"fields": {
"img": "http://id-live-02.slatic.net/p/2/case-anti-shock-anti-crack-elegant-softcase-for-xiaomi-redmi-note4x-white-clear-8431-85175402-0bec01e88741744ae5461c4b3a4ae160-catalog.jpg",
"quantity": {
"qtyPrefix": "Kuantitas",
"min": 1,
"autoOptions": false,
"quantity": 1,
"max": 5,
"editable": true,
"showIncrDecr": true,
"showOptions": false,
"step": 1
},
"sellerName": "incredible accessories hp",
"title": "Case Anti Shock / Anti Crack Elegant Softcase for Xiaomi Redmi Note 4x - White Clear",
"stockTip": {},
"valid": true,
"itemId": "108849535",
"operations": [
"wishlist",
"delete"
],
"sellerId": "53631",
"price": {
"price": 3977,
"currentPrice": "Rp3.977",
"originPrice": "Rp15.000",
"promotionRatio": "-73%"
},
"restriction": false,
"isGift": false,
"sku": {
"skuText": "Sarung, Bening",
"productVariant": "CA529ELAAC6GUEANID-27304198",
"brandId": "10464",
"skuId": "110628148"
},
"itemUrl": "https://www.lazada.co.id/products/i108849535-s110628148.html?urlFlag=true&mp=1",
"cartItemId": 2005983481
},
"type": "biz"
},
"package_pfdaf8a8a": {
"id": "pfdaf8a8a",
"tag": "package",
"fields": {},
"type": "biz"
}
},
"hierarchy": {
"component": [
"container",
"delivery",
"item",
"shop",
"package",
"listHeader",
"orderSummary",
"leftContainer",
"orderTotal",
"floatTips",
"root",
"location",
"rightContainer",
"voucherInput"
],
"root": "root_0",
"structure": {
"package_p43597338a": [
"delivery_d43597338a",
"shop_43597338a_s2c",
"item_i7799f86e",
"item_i7790e0f9"
],
"leftContainer_CL": [
"delivery_3",
"listHeader_H",
"package_pfdaf8a8a",
"package_p43597338a"
],
"container_C": [
"leftContainer_CL",
"rightContainer_CR"
],
"rightContainer_CR": [
"location_2",
"orderSummary_6"
],
"orderSummary_6": [
"voucherInput_7",
"orderTotal_8"
],
"root_0": [
"container_C",
"floatTips_4"
],
"package_pfdaf8a8a": [
"delivery_dfdaf8a8a",
"shop_fdaf8a8a_s23f9",
"item_i77997d6b"
]
}
},
"linkage": {
"input": [],
"request": [
"voucherInput_7",
"item_i7799f86e",
"delivery_3",
"orderTotal_8",
"item_i7790e0f9",
"location_2",
"item_i77997d6b"
],
"common": {
"compress": true,
"queryParams": "^^$$1afe141216814f45e9fc6dba84d4863d{$_$}H4sIAAAAAAAAAFWU247bRgyGXyXQ9caYGXJOe5fkJmmLot3de4Ez5OwKlSVBklMkQd69lJsNHBgwZIHfz9NPf+t2Wc/DRGN3/61bRtrbvJ67+26p3V230LPoc6V1777fdROd5XGh+vpu05DLJuu7bRuep0OgjrRtf2rYETGfTyN9JabTNfh0nlnG02Xc13k6LbTSeTv9fZH1y1/XZxVbZWJZ3w9ff7xRxXGutA/z9DTs46H6G/1Lb55keqaXu9+pnN68n7/MI43D3bvzIqOKEPMq2/aJNfrBQUrg49uHZIw1kMP/T97ZfPQ3b/sHrUoju+/aoJa8zJNM+/aoDdF+WeXa1TztNEyy9h800jUoDoPFWnMIVHwCMMiWCJ2zgVRX+xw+a2M9aHyMsbpqwcUChTgxeMZqbWzkJXl7G88IPkeARApKMCYQeJNYojCEVPTboMcCmOIvibgxtURXLrRaKGKrWdhHMslkE1ss2TjnCA+ujTPtT8Oy9XgkilVQsgEX2AXJgQ3UZCNb1yKEY1LDLud+iDEbMS0rYwAzx+KbE6JmcgCbs02YJShhyi2TI4eiTAHr0ROiZS0mOteYW9JChZwR+CVPbikce2Ev4HJiJmuab6TJIApIkZo4JFZmlKZb/LmiP66jqzVlEhe5GJEUs/XNk6csxYacDmrY9o9C6rf+oxJAHqAkJMc69RbZu+yBQKxFqTEcxA8r9u5wgagLRISDNuECYjAFk3NYUnU+Htczryr+eDmfSRcUlNEukSyVmqLXeNG9NnWF16RBV95emad5p7FPx5QNGnSUpHDUllsyGINQUKSp78L1Rus/eqb9cmsdgGp1vmpLcKjD1JsNTUGdqmu6olvuxjmlpaa7KV6wtkqU1UimOXA6gIjXk1mH55fbaT8olhJJMhFrrlwxRwuAmK00ELD6+8Dmee9Ndz9dxvGu217mpf9Zb7+5qirZ1YoYOXJlWw1r1Zz1w7kAxKvKlXutVzG4WlFvGUyKxkhB9RNL9ibmmoGTnkJFBT/Pl/oi66dpuex9VMa65LRQ0fRWnDjSZDogFZJkkYL+HfwHDyba3BsFAAA=",
"submitParams": "^^$$f7898231e0216a924bcaa64d213a61ba{$_$}H4sIAAAAAAAAADXMQQrDMAwEwL/oHPSA3PqBUsgLto4bDJJtJPvS4L/XCfS2uwx7UoumKUNoPakK2qeY0ko10EIVR5w5wBqNhTI0bhXhv/kk3aM93NORr4MgcH9OdomiLPhiB9+YtexRuEuzkrnCoM5bf2tqr7vQGD8eVpdVjwAAAA=="
},
"signature": "89dffd9ca23307e603556a4c896e4c56"
}
}
}
With jq I can do this:
[me#linux]$ cat /tmp/json | jq '.module | .data | keys'
[
"container_C",
"delivery_3",
"delivery_d43597338a",
"delivery_dfdaf8a8a",
"floatTips_4",
"item_i7790e0f9",
"item_i77997d6b",
"item_i7799f86e",
"leftContainer_CL",
"listHeader_H",
"location_2",
"orderSummary_6",
"orderTotal_8",
"package_p43597338a",
"package_pfdaf8a8a",
"rightContainer_CR",
"root_0",
"shop_43597338a_s2c",
"shop_fdaf8a8a_s23f9",
"voucherInput_7"
]
I need to get orderTotal_8 but please note that the number 8 is always changing, so it can be orderTotal_10 or orderTotal_3 etc... How can I get this key with pure jq command if possible, without the help of grep / awk ? So the result should be like this orderTotal_8
If you want to form a subarray of items satisfying some condition, just add map(select( CONDITION )) to the pipeline, e.g.
.module | .data | keys | map(select(test("^orderTotal_")))
If you just want a stream of the items matching the condition, then you could first form the stream, and then make the selection:
.module | .data | keys[] | select(test("^orderTotal_"))
For efficiency and other reasons, you might want to consider using keys_unsorted instead of keys.

how to sort a json file in shell script based on a given key?

I wanted to sort the file which have json data on basis of a key(fare) in a shell script (.sh) file.Is there a way to acheive the same ?
{ "route":[
{"match":"true","column":"10","fare":"120.0","source":"false","length":"1","name":"41","row":"4","width":"1","zIndex":"0"},
{"match":"true","column":"9","fare":"110.0","source":"false","length":"1","name":"37","row":"3","width":"1","zIndex":"0"},
{"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"},
{"match":"true","column":"7","fare":"510.0","source":"false","length":"1","name":"29","row":"1","width":"1","zIndex":"0"},
{"match":"true","column":"6","fare":"50.0","source":"false","length":"1","name":"29","row":"0","width":"1","zIndex":"0"}
]
};
jq '.route = (.route | sort_by(.fare))' file.json
{
"route": [
{
"zIndex": "0",
"match": "true",
"column": "9",
"fare": "110.0",
"source": "false",
"length": "1",
"name": "37",
"row": "3",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "10",
"fare": "120.0",
"source": "false",
"length": "1",
"name": "41",
"row": "4",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "6",
"fare": "50.0",
"source": "false",
"length": "1",
"name": "29",
"row": "0",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "8",
"fare": "500.0",
"source": "false",
"length": "1",
"name": "33",
"row": "2",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "7",
"fare": "510.0",
"source": "false",
"length": "1",
"name": "29",
"row": "1",
"width": "1"
}
]
}
Note that the sorting is lexical ("50" is in the middle). This is because the values are strings not numbers. To get numeric sorting, we need to cast as a number
jq '.route = (.route | sort_by(.fare | tonumber))' file.json
{
"route": [
{
"zIndex": "0",
"match": "true",
"column": "6",
"fare": "50.0",
"source": "false",
"length": "1",
"name": "29",
"row": "0",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "9",
"fare": "110.0",
"source": "false",
"length": "1",
"name": "37",
"row": "3",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "10",
"fare": "120.0",
"source": "false",
"length": "1",
"name": "41",
"row": "4",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "8",
"fare": "500.0",
"source": "false",
"length": "1",
"name": "33",
"row": "2",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "7",
"fare": "510.0",
"source": "false",
"length": "1",
"name": "29",
"row": "1",
"width": "1"
}
]
}

how to parse json body which is like a listed dictionary

i want to get access of "uuid" everytime,which is two times present in this code and also access to "cloud_uuid". the body is something like below:
{
"computes": [{
"uuid": "110c607a-231c-4724-be7f-db5ed388158",
"name": "9.4.98.33",
"description": null,
"version": "1.0",
"type": "compute",
"number_of_vms": 0,
"status": "ACTIVE",
"provisioning_status": {
"status": "COMPLETED",
"started_at": "",
"updated_at": "",
"status_data": null
},
"health_status": {
"status": "OK",
"alerts": [],
"updated_at": "2014-07-11T17:09:12.194000"
},
"compliance_status": {
"compliance_reasons": null,
"is_compliant": true,
"updated_at": null
},
"run_priority_order": null,
"created": "2014-07-11T16:01:32.837821",
"updated": "2014-07-11T17:08:16.031838",
"capability_categories": {
"v": [{
"name_key": "",
"description_key": "",
"version": "0",
"hidden": t,
"priority": 1,
"name_nls": "",
"description_nls": ""
}],
"monitoring": [{
"name_key": "m",
"description_key": null,
"version": "1.0",
"hidden": true,
"priority": 100,
"name_nls": "monitoring",
"description_nls": null
}],
"scheduler": [{
"name_key": "",
"description_key": null,
"version": "1.0",
"hidden": false,
"priority": 20,
"name_nls": "",
"description_nls": null
}],
"network": [{
"name_key": "",
"description_key": "",
"version": "1.0",
"hidden": false,
"priority": 10,
"name_nls": "",
"description_nls": ""
}]
},
"links": [{
"href": "",
"rel": "self"
}, {
"href": "",
"rel": "bookmark"
}],
"cloud_uuid": "b603e16e-38a6-435e-9359-79c27fee93a",
"operating_system_uuid": "70f605e7-6512-49b4-833c-b25d47823a4"
}, {
"uuid": "7383f4a5-dc0a-420b-806c-abbd49c1655a",
"name": "9.4.193.20",
"description": null,
"version": "1.0",
"type": "compute"
could you help with below,i tried the code as answered in comment:
suppose body is "clouds" instead of "computes"
i tried for getting cloud_uuid with something like:cloud_uuid = ((e['cloud_uuid'] for e in dict['clouds'] if e['name'] == name_to_find), None)
it throws error->
cloud_uuid = ((e['cloud_uuid'] for e in dict['clouds'] if e['name'] ==
name_to_find), None) TypeError: 'type' object is unsubscriptable
In python it is very simple:
[(e['uuid'], e['cloud_uuid']) for e in dict['computes']]
Edit:
It looks like I overlooked a pair square braces. Try:
[(_['computes'][0]['uuid'], _['cloud_uuid']) for _ in data]