Conditions In Jq tool - json

Greetings for the day!
how to get "created" value if "toString" value is Code Review from below json code.
Json code:-
{
"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
"id": "562940",
"self": "https://example/rest/api/2/issue/562940",
"key": "TPR-13098",
"fields": {
"customfield_17384": null,
"customfield_17379": null,
"customfield_17378": null,
"labels": [
],
"customfield_15505": null,
"issuelinks": [
],
"customfield_11709": null,
"assignee": {
"self": "https://example/rest/api/2/user?username=example",
"name": "example",
"key": "example",
"emailAddress": "example#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?ownerId=example&avatarId=12908",
"24x24": "https://example/secure/useravatar?size=small&ownerId=example&avatarId=12908",
"16x16": "https://example/secure/useravatar?size=xsmall&ownerId=example&avatarId=12908",
"32x32": "https://example/secure/useravatar?size=medium&ownerId=example&avatarId=12908"
},
"displayName": "example",
"active": true,
"timeZone": "Asia/Kolkata"
},
"components": [
{
"self": "https://example/rest/api/2/component/17139",
"id": "17139",
"name": "Others"
}
],
"customfield_17366": null,
"customfield_11935": [
{
"self": "https://example/rest/api/2/user?username=testuser",
"name": "testuser",
"key": "testuser",
"emailAddress": "testuser#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?avatarId=15907",
"24x24": "https://example/secure/useravatar?size=small&avatarId=15907",
"16x16": "https://example/secure/useravatar?size=xsmall&avatarId=15907",
"32x32": "https://example/secure/useravatar?size=medium&avatarId=15907"
},
"displayName": "testuser",
"active": true,
"timeZone": "Asia/Kolkata"
}
],
"customfield_11937": null,
"customfield_17351": "2021-06-30",
"subtasks": [
],
"customfield_17352": null,
"customfield_17359": null,
"reporter": {
"self": "https://example/rest/api/2/user?username=testuser",
"name": "testuser",
"key": "testuser",
"emailAddress": "testuser#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?avatarId=15907",
"24x24": "https://example/secure/useravatar?size=small&avatarId=15907",
"16x16": "https://example/secure/useravatar?size=xsmall&avatarId=15907",
"32x32": "https://example/secure/useravatar?size=medium&avatarId=15907"
},
"displayName": "testuser",
"active": true,
"timeZone": "Asia/Kolkata"
},
"customfield_17357": null,
"customfield_16700": null,
"customfield_11920": null,
"customfield_11922": null,
"progress": {
"progress": 0,
"total": 0
},
"worklog": {
"startAt": 0,
"maxResults": 20,
"total": 0,
"worklogs": [
]
},
"issuetype": {
"self": "https://example/rest/api/2/issuetype/12506",
"id": "12506",
"description": "",
"iconUrl": "https://example/secure/viewavatar?size=xsmall&avatarId=10306&avatarType=issuetype",
"name": "Work Item",
"subtask": false,
"avatarId": 10306
},
"customfield_17102": null,
"customfield_17347": null,
"project": {
"self": "https://example/rest/api/2/project/14309",
"id": "14309",
"key": "TPR",
"name": "TallyPrime",
"projectTypeKey": "software",
"avatarUrls": {
"48x48": "https://example/secure/projectavatar?avatarId=10703",
"24x24": "https://example/secure/projectavatar?size=small&avatarId=10703",
"16x16": "https://example/secure/projectavatar?size=xsmall&avatarId=10703",
"32x32": "https://example/secure/projectavatar?size=medium&avatarId=10703"
}
},
"customfield_17346": null,
"customfield_17103": null,
"customfield_17345": "-",
"customfield_15602": null,
"customfield_11486": null,
"customfield_15600": null,
"customfield_15601": null,
"customfield_16806": null,
"customfield_16804": null,
"customfield_11910": null,
"customfield_16803": null,
"customfield_11913": "2021-06-28",
"customfield_11912": null,
"customfield_11915": "2021-06-28",
"resolutiondate": null,
"customfield_11914": null,
"watches": {
"self": "https://example/rest/api/2/issue/TPR-13098/watchers",
"watchCount": 1,
"isWatching": false
},
"customfield_16002": null,
"customfield_17333": null,
"customfield_17331": 0.0,
"customfield_17330": null,
"customfield_16006": null,
"customfield_16005": null,
"customfield_16003": null,
"customfield_16802": null,
"customfield_16009": null,
"customfield_16801": null,
"customfield_16800": null,
"customfield_17339": null,
"customfield_16007": null,
"customfield_17338": null,
"updated": "2021-06-29T17:40:13.000+0530",
"customfield_17322": null,
"customfield_17321": null,
"timeoriginalestimate": null,
"customfield_17326": null,
"description": "Clean up of JsonElem::GetOwnName as it is duplicate of JsonElem::GetKey.",
"customfield_10010": "0|i2he8j:",
"customfield_17323": null,
"customfield_11344": 0.0,
"customfield_17329": null,
"timetracking": {
},
"customfield_15700": {
"self": "https://example/rest/api/2/customFieldOption/16104",
"value": "Thank you",
"id": "16104"
},
"customfield_10005": null,
"customfield_14726": null,
"customfield_10006": null,
"customfield_14724": null,
"customfield_14725": null,
"summary": "Clean up of GetOwnName from JsonElem class",
"customfield_16101": null,
"customfield_17311": null,
"customfield_16100": null,
"customfield_17310": null,
"customfield_16103": null,
"customfield_16102": null,
"customfield_17312": null,
"customfield_14722": null,
"customfield_14723": null,
"customfield_17318": null,
"customfield_14721": null,
"customfield_11204": null,
"customfield_11205": "0.0",
"environment": null,
"customfield_11206": 0.0,
"customfield_10118": null,
"duedate": null,
"comment": {
"comments": [
],
"maxResults": 0,
"total": 0,
"startAt": 0
},
"customfield_17300": null,
"customfield_17304": "3.Coding",
"customfield_17303": null,
"fixVersions": [
{
"self": "https://example/rest/api/2/version/18013",
"id": "18013",
"description": "This is for Release B1 of GST Revamp planned as part of TallyPrime",
"name": "TallyPrime Release 3.0",
"archived": false,
"released": false
}
],
"customfield_17301": {
"self": "https://example/rest/api/2/customFieldOption/18133",
"value": "Client",
"id": "18133"
},
"customfield_15800": null,
"customfield_17307": {
"self": "https://example/rest/api/2/user?username=example",
"name": "example",
"key": "example",
"emailAddress": "example#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?ownerId=example&avatarId=12908",
"24x24": "https://example/secure/useravatar?size=small&ownerId=example&avatarId=12908",
"16x16": "https://example/secure/useravatar?size=xsmall&ownerId=example&avatarId=12908",
"32x32": "https://example/secure/useravatar?size=medium&ownerId=example&avatarId=12908"
},
"displayName": "example",
"active": true,
"timeZone": "Asia/Kolkata"
},
"customfield_17305": null,
"customfield_14704": null,
"customfield_14705": null,
"timeestimate": null,
"status": {
"self": "https://example/rest/api/2/status/12606",
"description": "",
"iconUrl": "https://example/images/icons/statuses/generic.png",
"name": "Code Review",
"id": "12606",
"statusCategory": {
"self": "https://example/rest/api/2/statuscategory/4",
"id": 4,
"key": "indeterminate",
"colorName": "yellow",
"name": "In Progress"
}
},
"customfield_17403": null,
"customfield_17401": null,
"customfield_15102": {
"self": "https://example/rest/api/2/customFieldOption/18132",
"value": "Code Improvement",
"id": "18132"
},
"customfield_17400": null,
"customfield_15900": null,
"customfield_17406": null,
"customfield_17405": null,
"customfield_17404": null,
"customfield_14802": null,
"aggregatetimeestimate": null,
"creator": {
"self": "https://example/rest/api/2/user?username=testuser",
"name": "testuser",
"key": "testuser",
"emailAddress": "testuser#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?avatarId=15907",
"24x24": "https://example/secure/useravatar?size=small&avatarId=15907",
"16x16": "https://example/secure/useravatar?size=xsmall&avatarId=15907",
"32x32": "https://example/secure/useravatar?size=medium&avatarId=15907"
},
"displayName": "testuser",
"active": true,
"timeZone": "Asia/Kolkata"
},
"aggregateprogress": {
"progress": 0,
"total": 0
},
"timespent": null,
"customfield_15202": [
],
"customfield_15203": null,
"customfield_15200": null,
"customfield_15201": null,
"aggregatetimespent": null,
"customfield_15204": null,
"workratio": -1,
"customfield_15306": null,
"customfield_15303": null,
"attachment": [
],
"customfield_10527": null,
"customfield_10518": null
},
"changelog": {
"startAt": 0,
"maxResults": 4,
"total": 4,
"histories": [
{
"id": "3818773",
"author": {
"self": "https://example/rest/api/2/user?username=testuser",
"name": "testuser",
"key": "testuser",
"emailAddress": "testuser#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?avatarId=15907",
"24x24": "https://example/secure/useravatar?size=small&avatarId=15907",
"16x16": "https://example/secure/useravatar?size=xsmall&avatarId=15907",
"32x32": "https://example/secure/useravatar?size=medium&avatarId=15907"
},
"displayName": "testuser",
"active": true,
"timeZone": "Asia/Kolkata"
},
"created": "2021-06-28T16:34:02.000+0530",
"items": [
{
"field": "status",
"fieldtype": "jira",
"from": "10114",
"fromString": "New",
"to": "12605",
"toString": "Coding"
}
]
},
{
"id": "3818870",
"author": {
"self": "https://example/rest/api/2/user?username=testuser",
"name": "testuser",
"key": "testuser",
"emailAddress": "testuser#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?avatarId=15907",
"24x24": "https://example/secure/useravatar?size=small&avatarId=15907",
"16x16": "https://example/secure/useravatar?size=xsmall&avatarId=15907",
"32x32": "https://example/secure/useravatar?size=medium&avatarId=15907"
},
"displayName": "testuser",
"active": true,
"timeZone": "Asia/Kolkata"
},
"created": "2021-06-28T17:23:44.000+0530",
"items": [
{
"field": "status",
"fieldtype": "jira",
"from": "12605",
"fromString": "Coding",
"to": "12606",
"toString": "Code Review"
}
]
},
{
"id": "3820419",
"author": {
"self": "https://example/rest/api/2/user?username=example",
"name": "example",
"key": "example",
"emailAddress": "example#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?ownerId=example&avatarId=12908",
"24x24": "https://example/secure/useravatar?size=small&ownerId=example&avatarId=12908",
"16x16": "https://example/secure/useravatar?size=xsmall&ownerId=example&avatarId=12908",
"32x32": "https://example/secure/useravatar?size=medium&ownerId=example&avatarId=12908"
},
"displayName": "example",
"active": true,
"timeZone": "Asia/Kolkata"
},
"created": "2021-06-29T17:40:12.000+0530",
"items": [
{
"field": "Coding Due Date",
"fieldtype": "custom",
"from": null,
"fromString": null,
"to": "2021-06-30",
"toString": "30/Jun/21"
}
]
},
{
"id": "3820420",
"author": {
"self": "https://example/rest/api/2/user?username=example",
"name": "example",
"key": "example",
"emailAddress": "example#sample",
"avatarUrls": {
"48x48": "https://example/secure/useravatar?ownerId=example&avatarId=12908",
"24x24": "https://example/secure/useravatar?size=small&ownerId=example&avatarId=12908",
"16x16": "https://example/secure/useravatar?size=xsmall&ownerId=example&avatarId=12908",
"32x32": "https://example/secure/useravatar?size=medium&ownerId=example&avatarId=12908"
},
"displayName": "example",
"active": true,
"timeZone": "Asia/Kolkata"
},
"created": "2021-06-29T17:40:13.000+0530",
"items": [
{
"field": "assignee",
"fieldtype": "jira",
"from": "testuser",
"fromString": "testuser",
"to": "example",
"toString": "example"
}
]
}
]
}
}

..
| objects
| select(has("created") and any(.items[]; .toString == "Code Review"))
| .created
Research Effort
For future reference, please note that, since you've been a member of the SO community for over a year and have asked other questions, you should be familiar with the SO guidelines for asking appropriate questions. Apart from the "miminum reproducible example" guidelines already mentioned on this page, there should be some evidence of "research effort" as per
How much research effort is expected of Stack Overflow users?

Related

How to add key to json file using powershell

i have a json file like this:
[
{
"DisplayName": "Title1",
"SignInName": null,
"RoleDefinitionName": "Name1",
"RoleDefinitionId": "1111",
"ObjectId": "0111",
"ObjectType": "ServicePrincipal",
"CanDelegate": false,
"Description": null,
"ConditionVersion": null,
"Condition": null
},
{
"DisplayName": "Title2",
"SignInName": null,
"RoleDefinitionName": "Name2",
"RoleDefinitionId": "1111",
"ObjectId": "02222",
"ObjectType": "ServicePrincipal",
"CanDelegate": false,
"Description": null,
"ConditionVersion": null,
"Condition": null
},
{
"DisplayName": "Title3",
"SignInName": null,
"RoleDefinitionName": "Name3",
"RoleDefinitionId": "33333",
"ObjectId": "03333",
"ObjectType": "ServicePrincipal",
"CanDelegate": false,
"Description": null,
"ConditionVersion": null,
"Condition": null
}
]
I need to add a KEY at a start with all this json as a VALUE of that key using powershell script so it would look like this:
{
"ManagedIdentity": [
{
"DisplayName": "Title1",
"SignInName": null,
"RoleDefinitionName": "Name1",
"RoleDefinitionId": "1111",
"ObjectId": "0111",
"ObjectType": "ServicePrincipal",
"CanDelegate": false,
"Description": null,
"ConditionVersion": null,
"Condition": null
},
{
"DisplayName": "Title2",
"SignInName": null,
"RoleDefinitionName": "Name2",
"RoleDefinitionId": "1111",
"ObjectId": "02222",
"ObjectType": "ServicePrincipal",
"CanDelegate": false,
"Description": null,
"ConditionVersion": null,
"Condition": null
},
{
"DisplayName": "Title3",
"SignInName": null,
"RoleDefinitionName": "Name3",
"RoleDefinitionId": "33333",
"ObjectId": "03333",
"ObjectType": "ServicePrincipal",
"CanDelegate": false,
"Description": null,
"ConditionVersion": null,
"Condition": null
}
]
}
I should do it inside one script block, and i am not sure how to figure this out. It is a ready json file that i need to append key to.
The script that gets a file is something like this:
Get-AzRoleAssignment -ObjectId 02222 | ConvertTo-Json
to this short script using pipes i need to append a solution to append the key to the file.

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": []
}

Search JSON file bash

I have following JSON file and would like to extract part of it:
{
"expand": "names,schema",
"issues": [
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"fields": {
"aggregateprogress": {
"progress": 0,
"total": 0
},
"aggregatetimeestimate": null,
"aggregatetimeoriginalestimate": null,
"aggregatetimespent": null,
"assignee": {
"active": true,
"avatarUrls": {
"16x16": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16",
"24x24": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24",
"32x32": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32",
"48x48": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48"
},
"displayName": "user",
"emailAddress": "user#company.com",
"key": "user",
"name": "user",
"self": "https://jira.corp.company.com/rest/api/2/user?username=dvucanovic",
"timeZone": "Europe/Belgrade"
},
"components": [],
"created": "2018-03-06T21:24:41.000+0000",
"creator": {
"active": true,
"avatarUrls": {
"16x16": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16",
"24x24": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24",
"32x32": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32",
"48x48": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48"
},
"displayName": "user",
"emailAddress": "user#company.com",
"key": "user",
"name": "user",
"self": "https://jira.corp.company.com/rest/api/2/user?username=dvucanovic",
"timeZone": "Europe/Belgrade"
},
"customfield_10000": null,
"customfield_10001": null,
"customfield_10002": null,
"customfield_10004": "0|i00uta:",
"customfield_10005": null,
"customfield_10006": null,
"customfield_10100": null,
"customfield_10101": [],
"customfield_10102": null,
"customfield_10103": null,
"customfield_10107": {
"id": "10400",
"self": "https://jira.corp.company.com/rest/api/2/customFieldOption/10400",
"value": "Hentsu Internal"
},
"customfield_10108": null,
"customfield_10200": null,
"customfield_10201": "2018-03-06",
"customfield_10202": "2018-03-06",
"customfield_10203": null,
"customfield_10204": null,
"customfield_10205": null,
"customfield_10206": null,
"customfield_10300": "com.atlassian.servicedesk.plugins.approvals.internal.customfield.ApprovalsCFValue#5e6792fb",
"customfield_10301": null,
"customfield_10302": null,
"customfield_10600": null,
"customfield_10700": null,
"customfield_11000": null,
"customfield_11001": null,
"customfield_11002": null,
"customfield_11003": null,
"customfield_11004": null,
"customfield_11005": null,
"customfield_11006": null,
"customfield_11007": null,
"customfield_11008": null,
"customfield_11009": null,
"customfield_11010": null,
"customfield_11011": null,
"customfield_11012": null,
"customfield_11013": null,
"customfield_11014": null,
"customfield_11015": null,
"customfield_11016": null,
"customfield_11017": null,
"customfield_11018": null,
"customfield_11019": null,
"customfield_11100": null,
"customfield_11101": null,
"customfield_11102": null,
"description": null,
"duedate": null,
"environment": null,
"fixVersions": [],
"issuelinks": [],
"issuetype": {
"avatarId": 10318,
"description": "A task that needs to be done.",
"iconUrl": "https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype",
"id": "10100",
"name": "Task",
"self": "https://jira.corp.company.com/rest/api/2/issuetype/10100",
"subtask": false
},
"labels": [],
"lastViewed": "2018-03-06T21:31:34.315+0000",
"priority": {
"iconUrl": "https://jira.corp.company.com/images/icons/priorities/medium.svg",
"id": "3",
"name": "Medium",
"self": "https://jira.corp.company.com/rest/api/2/priority/3"
},
"progress": {
"progress": 0,
"total": 0
},
"project": {
"avatarUrls": {
"16x16": "https://jira.corp.company.com/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201",
"24x24": "https://jira.corp.company.com/secure/projectavatar?size=small&pid=10001&avatarId=10201",
"32x32": "https://jira.corp.company.com/secure/projectavatar?size=medium&pid=10001&avatarId=10201",
"48x48": "https://jira.corp.company.com/secure/projectavatar?pid=10001&avatarId=10201"
},
"id": "10001",
"key": "TECH",
"name": "Technology",
"self": "https://jira.corp.company.com/rest/api/2/project/10001"
},
"reporter": {
"active": true,
"avatarUrls": {
"16x16": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16",
"24x24": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24",
"32x32": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32",
"48x48": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48"
},
"displayName": "user",
"emailAddress": "user#company.com",
"key": "user",
"name": "user",
"self": "https://jira.corp.company.com/rest/api/2/user?username=dvucanovic",
"timeZone": "Europe/Belgrade"
},
"resolution": null,
"resolutiondate": null,
"status": {
"description": "",
"iconUrl": "https://jira.corp.company.com/",
"id": "10000",
"name": "Backlog",
"self": "https://jira.corp.company.com/rest/api/2/status/10000",
"statusCategory": {
"colorName": "blue-gray",
"id": 2,
"key": "new",
"name": "To Do",
"self": "https://jira.corp.company.com/rest/api/2/statuscategory/2"
}
},
"subtasks": [
{
"fields": {
"issuetype": {
"avatarId": 10316,
"description": "The sub-task of the issue",
"iconUrl": "https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype",
"id": "10101",
"name": "Sub-task",
"self": "https://jira.corp.company.com/rest/api/2/issuetype/10101",
"subtask": true
},
"priority": {
"iconUrl": "https://jira.corp.company.com/images/icons/priorities/medium.svg",
"id": "3",
"name": "Medium",
"self": "https://jira.corp.company.com/rest/api/2/priority/3"
},
"status": {
"description": "",
"iconUrl": "https://jira.corp.company.com/",
"id": "10000",
"name": "Backlog",
"self": "https://jira.corp.company.com/rest/api/2/status/10000",
"statusCategory": {
"colorName": "blue-gray",
"id": 2,
"key": "new",
"name": "To Do",
"self": "https://jira.corp.company.com/rest/api/2/statuscategory/2"
}
},
"summary": "Disable user account in Local AD"
},
"id": "16916",
"key": "TECH-728",
"self": "https://jira.corp.company.com/rest/api/2/issue/16916"
}
],
"summary": "disable user mm",
"timeestimate": null,
"timeoriginalestimate": null,
"timespent": null,
"updated": "2018-03-06T21:27:22.000+0000",
"versions": [],
"votes": {
"hasVoted": false,
"self": "https://jira.corp.company.com/rest/api/2/issue/TECH-726/votes",
"votes": 0
},
"watches": {
"isWatching": true,
"self": "https://jira.corp.company.com/rest/api/2/issue/TECH-726/watchers",
"watchCount": 1
},
"workratio": -1
},
"id": "16914",
"key": "TECH-726",
"self": "https://jira.corp.company.com/rest/api/2/issue/16914"
}
],
"maxResults": 50,
"startAt": 0,
"total": 1
}
How to extract only part in subtasks section
(subtask:true) ?
tried with:
jq -r '.issues[] | .fields.issuetype.subtask' 1.json # and
jq -r '.subtasks[] |.fields.issuetype.subtask' 1.json
but always get false
jq -r '.subtasks[] | .fields.issuetype.subtask' 1.json
jq: error (at 1.json:231): Cannot iterate over null (null)
jq solution:
jq '.issues[].fields.subtasks[].fields | .issuetype.subtask, .summary' 1.json
The output:
true
"Disable user account in Local AD"
With respect to a best-practice mechanism to iterate over output in bash (assuming, here, that the subtask field will never contain a literal tab, and that summary will never contain a literal newline):
while IFS=$'\t' read -r subtask summary; do
echo "Got subtask $subtask and summary $summary"
done < <(jq -r '.issues[].fields.subtasks[].fields
| [.issuetype.subtask, .summary] | #tsv')
If you want to avoid those assumptions, you can do even better by using NUL delimiters, which will correctly handle all values which can potentially be stored in a variable in bash:
while IFS= read -r -d '' subtask && IFS= read -r -d '' summary; do
printf 'Got subtask %q and summary %q\n' "$subtask" "$summary"
done < <(jq -j '.issues[].fields.subtasks[].fields
| (.issuetype.subtask, "\u0000", .summary, "\u0000")')
Community Wiki since this answer is intended to address an issue observed in comments on the accepted answer, rather than addressing the original question as such.

Searching JSON file using bash

i have JSON file and want to extract only some values
How to extract only "key": "TECH-456",
and "key": "TECH", (values after key)
{
"expand": "names,schema",
"issues": [
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"fields": {
"aggregateprogress": {
"percent": 100,
"progress": 86400,
"total": 86400
},
"aggregatetimeestimate": 0,
"aggregatetimeoriginalestimate": 57600,
"aggregatetimespent": 86400,
"assignee": {
"active": true,
"avatarUrls": {
"16x16": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16",
"24x24": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24",
"32x32": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32",
"48x48": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48"
},
"displayName": "user1",
"emailAddress": "user1#gmail.com",
"key": "user1",
"name": "user1",
"self": "https://mycompany/rest/api/2/user?username=user1",
"timeZone": "Europe/Belgrade"
},
"components": [],
"created": "2018-01-10T10:23:01.000+0000",
"creator": {
"active": true,
"avatarUrls": {
"16x16": "https://mycompany/secure/useravatar?size=xsmall&avatarId=10349",
"24x24": "https://mycompany/secure/useravatar?size=small&avatarId=10349",
"32x32": "https://mycompany/secure/useravatar?size=medium&avatarId=10349",
"48x48": "https://mycompany/secure/useravatar?avatarId=10349"
},
"displayName": "user2",
"emailAddress": "user2#gmail.com",
"key": "user2",
"name": "user2",
"self": "https://mycompany/rest/api/2/user?username=user2",
"timeZone": "Zulu"
},
"customfield_10000": null,
"customfield_10001": null,
"customfield_10002": null,
"customfield_10004": "0|i00n0f:",
"customfield_10005": null,
"customfield_10006": null,
"customfield_10100": null,
"customfield_10101": [],
"customfield_10102": null,
"customfield_10103": null,
"customfield_10107": {
"id": "10400",
"self": "https://mycompany/rest/api/2/customFieldOption/10400",
"value": "Hentsu Internal"
},
"customfield_10108": null,
"customfield_10200": null,
"customfield_10201": "2018-01-12",
"customfield_10202": "2018-01-12",
"customfield_10203": null,
"customfield_10204": null,
"customfield_10205": null,
"customfield_10206": null,
"customfield_10300": "com.atlassian.servicedesk.plugins.approvals.internal.customfield.ApprovalsCFValue#40efdb56",
"customfield_10301": null,
"customfield_10302": null,
"customfield_10600": null,
"customfield_10700": null,
"customfield_11000": null,
"customfield_11001": null,
"customfield_11002": null,
"customfield_11003": null,
"customfield_11004": null,
"customfield_11005": null,
"customfield_11006": null,
"customfield_11007": null,
"customfield_11008": null,
"customfield_11009": null,
"customfield_11010": null,
"customfield_11011": null,
"customfield_11012": null,
"customfield_11013": null,
"customfield_11014": null,
"customfield_11015": null,
"customfield_11016": null,
"customfield_11017": null,
"customfield_11018": null,
"customfield_11019": null,
"customfield_11100": null,
"customfield_11101": null,
"customfield_11102": null,
"description": ".",
"duedate": null,
"environment": null,
"fixVersions": [],
"issuelinks": [],
"issuetype": {
"avatarId": 10318,
"description": "A task that needs to be done.",
"iconUrl": "https://mycompany/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype",
"id": "10100",
"name": "Task",
"self": "https://mycompany/rest/api/2/issuetype/10100",
"subtask": false
},
"labels": [],
"lastViewed": "2018-02-22T15:47:47.792+0000",
"priority": {
"iconUrl": "https://mycompany/images/icons/priorities/medium.svg",
"id": "3",
"name": "Medium",
"self": "https://mycompany/rest/api/2/priority/3"
},
"progress": {
"percent": 100,
"progress": 86400,
"total": 86400
},
"project": {
"avatarUrls": {
"16x16": "https://mycompany/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201",
"24x24": "https://mycompany/secure/projectavatar?size=small&pid=10001&avatarId=10201",
"32x32": "https://mycompany/secure/projectavatar?size=medium&pid=10001&avatarId=10201",
"48x48": "https://mycompany/secure/projectavatar?pid=10001&avatarId=10201"
},
"id": "10001",
"key": "TECH",
"name": "Technology",
"self": "https://mycompany/rest/api/2/project/10001"
},
"reporter": {
"active": true,
"avatarUrls": {
"16x16": "https://mycompany/secure/useravatar?size=xsmall&avatarId=10349",
"24x24": "https://mycompany/secure/useravatar?size=small&avatarId=10349",
"32x32": "https://mycompany/secure/useravatar?size=medium&avatarId=10349",
"48x48": "https://mycompany/secure/useravatar?avatarId=10349"
},
"displayName": "user2",
"emailAddress": "user2#gmail.com",
"key": "user2",
"name": "user2",
"self": "https://mycompany/rest/api/2/user?username=user2",
"timeZone": "Zulu"
},
"resolution": null,
"resolutiondate": null,
"status": {
"description": "",
"iconUrl": "https://mycompany/images/icons/statuses/inprogress.png",
"id": "3",
"name": "In-Progress",
"self": "https://mycompany/rest/api/2/status/3",
"statusCategory": {
"colorName": "yellow",
"id": 4,
"key": "indeterminate",
"name": "In Progress",
"self": "https://mycompany/rest/api/2/statuscategory/4"
}
},
"subtasks": [],
"summary": "Build the Fortimanage Portal",
"timeestimate": 0,
"timeoriginalestimate": 57600,
"timespent": 86400,
"updated": "2018-01-31T15:22:06.000+0000",
"versions": [],
"votes": {
"hasVoted": false,
"self": "https://mycompany/rest/api/2/issue/TECH-456/votes",
"votes": 0
},
"watches": {
"isWatching": false,
"self": "https://mycompany/rest/api/2/issue/TECH-456/watchers",
"watchCount": 1
},
"workratio": 150
},
"id": "15377",
"key": "TECH-456",
"self": "https://mycompany/rest/api/2/issue/15377"
}
],
"maxResults": 50,
"startAt": 0,
"total": 1
}
The right way with jq tool:
jq -r '.issues[0] | .key, .fields.project.key' file.json
The output:
TECH-456
TECH

How to loop through JSON in ruby

Below details is my shipment details. I want to loop through this JSON and get each checkpoint details.
How can I do this with Ruby
This is my controller:
#asd = AfterShip::V4::Tracking.get('ups', '1Z31Y1Y90490064644')
this is my view page where i get json data
= #asd.to_json`
This is the JSON:
{
"meta": {
"code": 200
},
"data": {
"tracking": {
"id": "560b87b38c9079f272e98dfc",
"created_at": "2015-09-30T06:56:51+00:00",
"updated_at": "2015-09-30T06:56:55+00:00",
"last_updated_at": "2015-09-30T06:56:55+00:00",
"tracking_number": "1Z31Y1Y90490064644",
"slug": "ups",
"active": false,
"android": [
],
"custom_fields": null,
"customer_name": null,
"delivery_time": 7,
"destination_country_iso3": "GBR",
"emails": [
"pu#l.com"
],
"expected_delivery": null,
"ios": [
],
"note": null,
"order_id": null,
"order_id_path": null,
"origin_country_iso3": "HKG",
"shipment_package_count": 1,
"shipment_pickup_date": "2015-08-24T16:00:00",
"shipment_delivery_date": "2015-09-01T14:22:00",
"shipment_type": "UPS SAVER",
"shipment_weight": 0.5,
"shipment_weight_unit": "kg",
"signed_by": "MANTON (RESIDENTIAL)",
"smses": [
],
"source": "api",
"tag": "Delivered",
"title": "1Z31Y1Y90490064644",
"tracked_count": 1,
"unique_token": "bkGcaTOJDe",
"checkpoints": [
{
"slug": "ups",
"city": null,
"created_at": "2015-09-30T06:56:55+00:00",
"location": "HK",
"country_name": "HK",
"message": "BILLING INFORMATION RECEIVED",
"country_iso3": "HKG",
"tag": "InfoReceived",
"checkpoint_time": "2015-08-25T23:05:47",
"coordinates": [
],
"state": null,
"zip": null
},
{
"slug": "ups",
"city": null,
"created_at": "2015-09-30T06:56:55+00:00",
"location": null,
"country_name": null,
"message": "YOUR PACKAGE WAS RELEASED BY THE CLEARING AGENCY.",
"country_iso3": null,
"tag": "InTransit",
"checkpoint_time": "2015-08-27T16:57:00",
"coordinates": [
],
"state": null,
"zip": null
},
{
"slug": "ups",
"city": null,
"created_at": "2015-09-30T06:56:55+00:00",
"location": null,
"country_name": null,
"message": "YOUR PACKAGE WAS RELEASED BY THE CLEARING AGENCY.",
"country_iso3": null,
"tag": "InTransit",
"checkpoint_time": "2015-08-27T17:07:00",
"coordinates": [
],
"state": null,
"zip": null
}
}
}
}
What you are getting here is an instance of Hash class (the response JSON is already parsed).
tracking = AfterShip::V4::Tracking.get('ups', '1Z31Y1Y90490064644')
tracking['data']['tracking']['id']
# => "560b87b38c9079f272e98dfc"