Related
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.
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?
I am using a COVID-19 API (https://api.covidactnow.org/v2/counties.json?apiKey=) that records various information like number of cases and deaths in each county in USA. In my React Native app, I retrieved the user's location, and more specifically, the user's county, state and country. In this case, I have stored the user's county name in countyName. I'd like to fetch the specific cases and deaths from the JSON file depending on what is countyName is. I have a snippet of one county in the JSON file below. I was thinking I could go into the index of the array and in the actuals, and then fetch the cases and deaths.
[
{
"fips": "02013",
"country": "US",
"state": "AK",
"county": "Aleutians East Borough",
"level": "county",
"lat": null,
"locationId": "iso1:us#iso2:us-ak#fips:02013",
"long": null,
"population": 3337,
"metrics": {
"testPositivityRatio": null,
"testPositivityRatioDetails": {
"source": "other"
},
"caseDensity": 0,
"contactTracerCapacityRatio": null,
"infectionRate": 4.71887277063,
"infectionRateCI90": 1.1615979428900003,
"icuHeadroomRatio": null,
"icuHeadroomDetails": null,
"icuCapacityRatio": null,
"vaccinationsInitiatedRatio": null,
"vaccinationsCompletedRatio": 0.27509739286784535
},
"riskLevels": {
"overall": 0,
"testPositivityRatio": 4,
"caseDensity": 0,
"contactTracerCapacityRatio": 4,
"infectionRate": 3,
"icuHeadroomRatio": 4,
"icuCapacityRatio": 4
},
"actuals": {
"cases": 363,
"deaths": 2,
"positiveTests": null,
"negativeTests": null,
"contactTracers": null,
"hospitalBeds": {
"capacity": null,
"currentUsageTotal": null,
"currentUsageCovid": null,
"typicalUsageRate": null
},
"icuBeds": {
"capacity": null,
"currentUsageTotal": null,
"currentUsageCovid": null,
"typicalUsageRate": null
},
"newCases": 0,
"newDeaths": 0,
"vaccinesDistributed": null,
"vaccinationsInitiated": null,
"vaccinationsCompleted": 918,
"vaccinesAdministered": null,
"vaccinesAdministeredDemographics": null,
"vaccinationsInitiatedDemographics": null
},
"annotations": {
"cases": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"deaths": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"positiveTests": null,
"negativeTests": null,
"contactTracers": null,
"hospitalBeds": null,
"icuBeds": null,
"newCases": null,
"newDeaths": null,
"vaccinesDistributed": null,
"vaccinationsInitiated": null,
"vaccinationsCompleted": {
"sources": [
{
"type": "other",
"url": "https://covid.cdc.gov/covid-data-tracker/#county-view",
"name": "Centers for Disease Control and Prevention"
}
],
"anomalies": []
},
"vaccinesAdministered": null,
"testPositivityRatio": null,
"caseDensity": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"contactTracerCapacityRatio": null,
"infectionRate": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"infectionRateCI90": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"icuHeadroomRatio": null,
"icuCapacityRatio": null,
"vaccinationsInitiatedRatio": null,
"vaccinationsCompletedRatio": null
},
"lastUpdatedDate": "2021-04-26",
"url": "https://covidactnow.org/us/alaska-ak/county/aleutians_east_borough"
},
The response that you got from the API in JSON format is an array of objects.
To get the no of deaths and cases from the array of objects, you need to do the following:
const response= [
{
"fips": "02013",
"country": "US",
"state": "AK",
"county": "Aleutians East Borough",
"level": "county",
"lat": null,
"locationId": "iso1:us#iso2:us-ak#fips:02013",
"long": null,
"population": 3337,
"metrics": {
"testPositivityRatio": null,
"testPositivityRatioDetails": {
"source": "other"
},
"caseDensity": 0,
"contactTracerCapacityRatio": null,
"infectionRate": 4.71887277063,
"infectionRateCI90": 1.1615979428900003,
"icuHeadroomRatio": null,
"icuHeadroomDetails": null,
"icuCapacityRatio": null,
"vaccinationsInitiatedRatio": null,
"vaccinationsCompletedRatio": 0.27509739286784535
},
"riskLevels": {
"overall": 0,
"testPositivityRatio": 4,
"caseDensity": 0,
"contactTracerCapacityRatio": 4,
"infectionRate": 3,
"icuHeadroomRatio": 4,
"icuCapacityRatio": 4
},
"actuals": {
"cases": 363,
"deaths": 2,
"positiveTests": null,
"negativeTests": null,
"contactTracers": null,
"hospitalBeds": {
"capacity": null,
"currentUsageTotal": null,
"currentUsageCovid": null,
"typicalUsageRate": null
},
"icuBeds": {
"capacity": null,
"currentUsageTotal": null,
"currentUsageCovid": null,
"typicalUsageRate": null
},
"newCases": 0,
"newDeaths": 0,
"vaccinesDistributed": null,
"vaccinationsInitiated": null,
"vaccinationsCompleted": 918,
"vaccinesAdministered": null,
"vaccinesAdministeredDemographics": null,
"vaccinationsInitiatedDemographics": null
},
"annotations": {
"cases": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"deaths": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"positiveTests": null,
"negativeTests": null,
"contactTracers": null,
"hospitalBeds": null,
"icuBeds": null,
"newCases": null,
"newDeaths": null,
"vaccinesDistributed": null,
"vaccinationsInitiated": null,
"vaccinationsCompleted": {
"sources": [
{
"type": "other",
"url": "https://covid.cdc.gov/covid-data-tracker/#county-view",
"name": "Centers for Disease Control and Prevention"
}
],
"anomalies": []
},
"vaccinesAdministered": null,
"testPositivityRatio": null,
"caseDensity": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"contactTracerCapacityRatio": null,
"infectionRate": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"infectionRateCI90": {
"sources": [
{
"type": "NYTimes",
"url": "https://github.com/nytimes/covid-19-data",
"name": "The New York Times"
}
],
"anomalies": []
},
"icuHeadroomRatio": null,
"icuCapacityRatio": null,
"vaccinationsInitiatedRatio": null,
"vaccinationsCompletedRatio": null
},
"lastUpdatedDate": "2021-04-26",
"url": "https://covidactnow.org/us/alaska-ak/county/aleutians_east_borough"
}
]
const enteredCountryName = "US";
//Getting the required country name from the array of objects using filter method
const countryArray = response.filter(item => item.country === enteredCountryName)
const noOfDeaths = countryArray[0].actuals.deaths;
const noOfCases = countryArray[0].actuals.cases;
console.log({noOfDeaths, noOfCases})
If you are only interested in a specific county, there is an endpoint for this in the API. Just query this, then there is no need to filter the fetched data further:
https://api.covidactnow.org/v2/county/{fips}.json?apiKey=YOUR_KEY_HERE
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.
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