json - query with conditions - json

I have following json:
{
"id": "1",
"name": "profile1",
"userId": "0",
"groupId": "3",
"attributes": [
{
"id": "104",
"name": "Enable",
"value": "1"
},
{
"id": "105",
"name": "TargetNode",
"value": "system1"
},
{
"id": "106",
"name": "Timeout",
"value": "30"
}
],
"xconns": [
{
"id": "1",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
},
{
"id": "1",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
},
{
"id": "1",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
}
]
}
{
"id": "2",
"name": "profile2",
"userId": "7",
"groupId": "0",
"attributes": [
{
"id": "104",
"name": "Enable",
"value": "1"
},
{
"id": "105",
"name": "TargetNode",
"value": "system2"
},
{
"id": "106",
"name": "Timeout",
"value": "30"
}
],
"xconns": [
{
"id": "2",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
},
{
"id": "2",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
},
{
"id": "2",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
}
]
}
I can filter following:
$ jq -r 'select([.attributes[] | .name == "TargetNode" ] | any ) | [{userId, groupId, id, name}] | .[] | if (.userId == "0") then del(.userId) else . end | if (.groupId == "0") then del(.groupId) else . end | to_entries | map("\(.key | ascii_upcase):\(.value)") | #tsv' file.json
GROUPID:3 ID:1 NAME:profile1
USERID:7 ID:2 NAME:profile2
I need to add also value of TargetNode:
GROUPID:3 ID:1 NAME:profile1 TARGETNODE:system1
USERID:7 ID:2 NAME:profile2 TARGETNODE:system2
is there a way to include it in
[{userId, groupId, id, name, TargetNode}]
to get the value of TargetNode and not null?
GROUPID:3 ID:1 NAME:profile1 TARGETNODE:null
USERID:7 ID:2 NAME:profile2 TARGETNODE:null
Update:
the solution provided by RomanPerekhrest is nearly ok, but there is one issue because the json file in real is much bigger, there are more attrobutes in "main secttion", for example:
{
"id": "1",
"name": "profile1",
"userId": "0",
"groupId": "3",
"attrib101": "A",
"attrib102": "B",
"attributes": [
...
...
it is cousing that RomanPerekhrest's jq filter returns too much...
how to rid of them too?
ID:1 NAME:profile1 GROUPID:3 ATTRIB101:A ATTRIB102:B TARGETNODE:system1
ID:2 NAME:profile2 USERID:7 ATTRIB101:C ATTRIB102:D TARGETNODE:system2

jq solution:
jq -r '.attributes |= map(select(.name == "TargetNode"))
| if (.attributes | length != 0) then .targetNode = .attributes[0].value else . end
| if (.userId == "0") then del(.userId) else . end
| if (.groupId == "0") then del(.groupId) else . end
| del(.attributes, .xconns) | to_entries
| map("\(.key | ascii_upcase):\(.value)") | #tsv' file.json
If an object with "name": "TargetNode" pair not exists - TARGETNODE won't be added into resulting structure
The output:
ID:1 NAME:profile1 GROUPID:3 TARGETNODE:system1
ID:2 NAME:profile2 USERID:7 TARGETNODE:system2

Related

Cannot get jq to query json object [duplicate]

This question already has answers here:
How to use jq when the variable has reserved characters?
(3 answers)
Closed 6 months ago.
I have a JSON file that I am trying to query with jq. I am unable to retrieve the observations. I am trying to retieve each of the "observations using the following command and not able to get to the result:
cat sample3.json | jq .dataSets[0].series.0:0:0:0:0.observations.0[0]
I am able to retieve up to the series using:
cat sample3.json | jq .dataSets[0].series
But once I try to drill down further I am getting a compile error:
$ cat sample3.json | jq .dataSets[0].series.0:0:0:0:0
jq: error: syntax error, unexpected LITERAL, expecting end of file (Unix shell quoting issues?) at <top-level>, line 1:
.dataSets[0].series.0:0:0:0:0
jq: 1 compile error
I am not sure what I am doing wrong here....
The input file is:
{
"header": {
"id": "b8be2cd5-33bf-4687-9e81-eb032f6f8a71",
"test": false,
"prepared": "2022-09-01T13:30:57.013+02:00",
"sender": {
"id": "ECB"
}
},
"dataSets": [
{
"action": "Replace",
"validFrom": "2022-09-01T13:30:57.013+02:00",
"series": {
"0:0:0:0:0": {
"attributes": [
0,
null,
0,
null,
null,
null,
null,
null,
null,
null,
null,
null,
0,
null,
0,
null,
0,
0,
0,
0
],
"observations": {
"0": [
1.4529,
0,
0,
null,
null
],
"1": [
1.4472,
0,
0,
null,
null
],
"2": [
1.4591,
0,
0,
null,
null
]
}
}
}
}
],
"structure": {
"links": [
{
"title": "Exchange Rates",
"rel": "dataflow",
"href": "https://sdw-wsrest.ecb.europa.eu:443/service/dataflow/ECB/EXR/1.0"
}
],
"name": "Exchange Rates",
"dimensions": {
"series": [
{
"id": "FREQ",
"name": "Frequency",
"values": [
{
"id": "D",
"name": "Daily"
}
]
},
{
"id": "CURRENCY",
"name": "Currency",
"values": [
{
"id": "AUD",
"name": "Australian dollar"
}
]
},
{
"id": "CURRENCY_DENOM",
"name": "Currency denominator",
"values": [
{
"id": "EUR",
"name": "Euro"
}
]
},
{
"id": "EXR_TYPE",
"name": "Exchange rate type",
"values": [
{
"id": "SP00",
"name": "Spot"
}
]
},
{
"id": "EXR_SUFFIX",
"name": "Series variation - EXR context",
"values": [
{
"id": "A",
"name": "Average"
}
]
}
],
"observation": [
{
"id": "TIME_PERIOD",
"name": "Time period or range",
"role": "time",
"values": [
{
"id": "2022-08-29",
"name": "2022-08-29",
"start": "2022-08-29T00:00:00.000+02:00",
"end": "2022-08-29T23:59:59.999+02:00"
},
{
"id": "2022-08-30",
"name": "2022-08-30",
"start": "2022-08-30T00:00:00.000+02:00",
"end": "2022-08-30T23:59:59.999+02:00"
},
{
"id": "2022-08-31",
"name": "2022-08-31",
"start": "2022-08-31T00:00:00.000+02:00",
"end": "2022-08-31T23:59:59.999+02:00"
}
]
}
]
},
"attributes": {
"series": [
{
"id": "TIME_FORMAT",
"name": "Time format code",
"values": [
{
"name": "P1D"
}
]
},
{
"id": "BREAKS",
"name": "Breaks",
"values": []
},
{
"id": "COLLECTION",
"name": "Collection indicator",
"values": [
{
"id": "A",
"name": "Average of observations through period"
}
]
},
{
"id": "COMPILING_ORG",
"name": "Compiling organisation",
"values": []
},
{
"id": "DISS_ORG",
"name": "Data dissemination organisation",
"values": []
},
{
"id": "DOM_SER_IDS",
"name": "Domestic series ids",
"values": []
},
{
"id": "PUBL_ECB",
"name": "Source publication (ECB only)",
"values": []
},
{
"id": "PUBL_MU",
"name": "Source publication (Euro area only)",
"values": []
},
{
"id": "PUBL_PUBLIC",
"name": "Source publication (public)",
"values": []
},
{
"id": "UNIT_INDEX_BASE",
"name": "Unit index base",
"values": []
},
{
"id": "COMPILATION",
"name": "Compilation",
"values": []
},
{
"id": "COVERAGE",
"name": "Coverage",
"values": []
},
{
"id": "DECIMALS",
"name": "Decimals",
"values": [
{
"id": "4",
"name": "Four"
}
]
},
{
"id": "NAT_TITLE",
"name": "National language title",
"values": []
},
{
"id": "SOURCE_AGENCY",
"name": "Source agency",
"values": [
{
"id": "4F0",
"name": "European Central Bank (ECB)"
}
]
},
{
"id": "SOURCE_PUB",
"name": "Publication source",
"values": []
},
{
"id": "TITLE",
"name": "Title",
"values": [
{
"name": "Australian dollar/Euro"
}
]
},
{
"id": "TITLE_COMPL",
"name": "Title complement",
"values": [
{
"name": "ECB reference exchange rate, Australian dollar/Euro, 2:15 pm (C.E.T.)"
}
]
},
{
"id": "UNIT",
"name": "Unit",
"values": [
{
"id": "AUD",
"name": "Australian dollar"
}
]
},
{
"id": "UNIT_MULT",
"name": "Unit multiplier",
"values": [
{
"id": "0",
"name": "Units"
}
]
}
],
"observation": [
{
"id": "OBS_STATUS",
"name": "Observation status",
"values": [
{
"id": "A",
"name": "Normal value"
}
]
},
{
"id": "OBS_CONF",
"name": "Observation confidentiality",
"values": [
{
"id": "F",
"name": "Free"
}
]
},
{
"id": "OBS_PRE_BREAK",
"name": "Pre-break observation value",
"values": []
},
{
"id": "OBS_COM",
"name": "Observation comment",
"values": []
}
]
}
}
}
The .foo syntax cannot be used if the key name has anything but alphanumeric characters or the underscore, or if the first character of the key name is numeric.
Assuming you are using a recent version of jq,
you can always use the form: ."foo", which is actually an abbreviation of the basic form, .["foo"].
So assuming you're using a sufficiently recent version of jq, your query could begin with:
.dataSets[0].series."0:0:0:0:0"
If you are presenting the jq query on a command line, then you may have to escape the double-quotes appropriately, e.g. in a bash shell, by enclosing the jq query in single-quotes.

Remove values of a string in a JSON file

I have a JSON file containing the following:
[
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
}
]
I would like to remove the values after date, number and ID. I would like to do this using a powershell script.
My expected outcome is:
[
{
"Date": "",
"Number": "",
"ID": ""
},
{
"Date": "",
"Number": "",
"ID": ""
},
{
"Date": "",
"Number": "",
"ID": ""
},
}
]
Can someone help me to achieve this? Kind Regards!
I would suggest iterating over the array and just set the properties to an empty string:
#'
[
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
]
'# | ConvertFrom-Json | ForEach-Object {
$_.Date = [string]::Empty
$_.Number = [string]::Empty
$_.ID = [string]::Empty
$_
} | ConvertTo-Json

How to avoid generating all combinations of selected data while constructing an object?

My original JSON is given below.
[
{
"id": "1",
"name": "AA_1",
"total": "100002",
"files": [
{
"filename": "8665b987ab48511eda9e458046fbc42e.csv",
"filename_original": "some.csv",
"status": "3",
"total": "100002",
"time": "2020-08-24 23:25:49"
}
],
"status": "3",
"created": "2020-08-24 23:25:49",
"filenames": "8665b987ab48511eda9e458046fbc42e.csv",
"is_append": "0",
"is_deleted": "0",
"comment": null
},
{
"id": "4",
"name": "AA_2",
"total": "43806503",
"files": [
{
"filename": "1b4812fe634938928953dd40db1f70b2.csv",
"filename_original": "other.csv",
"status": "3",
"total": "21903252",
"time": "2020-08-24 23:33:43"
},
{
"filename": "63ab85fef2412ce80ae8bd018497d8bf.csv",
"filename_original": "some.csv",
"status": "2",
"total": 0,
"time": "2020-08-24 23:29:30"
}
],
"status": "2",
"created": "2020-08-24 23:35:51",
"filenames": "1b4812fe634938928953dd40db1f70b2.csv&&63ab85fef2412ce80ae8bd018497d8bf.csv",
"is_append": "0",
"is_deleted": "0",
"comment": null
}
]
From this JSON I want to create new objects by combining fields from objects which have status: 2 and their files which also have the same pair, status: 2.
So, I am expecting a JSON array as below.
[
{
"id": "4",
"name": "AA_2",
"file_filename": "63ab85fef2412ce80ae8bd018497d8bf.csv",
"file_status": 2
}
]
So far I tried with this JQ filter:
.[]|select(.status=="2")|[{id:.id,file_filename:.files[].filename,file_status:.files[].status}]
But this produces some invalid data.
[
{
"id": "4", # want to remove this as file.status != 2
"file_filename": "1b4812fe634938928953dd40db1f70b2.csv",
"file_status": "3"
},
{
"id": "4",
"file_filename": "1b4812fe634938928953dd40db1f70b2.csv",
"file_status": "2"
},
{
"id": "4", # Repeat
"file_filename": "63ab85fef2412ce80ae8bd018497d8bf.csv",
"file_status": "3"
},
{
"id": "4", # Repeat
"file_filename": "63ab85fef2412ce80ae8bd018497d8bf.csv",
"file_status": "2"
}
]
How do I filter the new JSON using JQ and remove these duplicate objects?
By applying [] operator to files twice, you're running into a combinatorial explosion. That needs to be avoided, for example:
[ .[] | select(.status == "2") | {id, name} + (.files[] | select(.status == "2") | {file_filename: .filename, file_status: .status}) ]
Online demo

How to extract the path info?

For the following JSON, I'd like to extract something like this ( is a TAB character).
CHROMOSOMES<TAB>HUMAN<TAB>1<TAB>1
...
STATUSES<TAB>name<TAB>Approved
...
ATTRIBUTES<TAB>HGNC<TAB>HGNC ID<TAB>gd_hgnc_id
...
ATTRIBUTES<TAB>EXTERNAL<TAB>NCBI Gene ID<TAB>md_eg_id<TAB>NCBI
...
ORDER_BY<TAB>HGNC ID<TAB>gd_hgnc_id
...
I'd like a smart way to extract the path info of this tree structure. Could you anybody show me the best way to do so? Thanks.
{
"CHROMOSOMES": {
"HUMAN": [
{
"name": "1",
"value": "1"
},
{
"name": "2",
"value": "2"
},
{
"name": "3",
"value": "3"
},
{
"name": "4",
"value": "4"
},
{
"name": "5",
"value": "5"
},
{
"name": "6",
"value": "6"
},
{
"name": "7",
"value": "7"
},
{
"name": "8",
"value": "8"
},
{
"name": "9",
"value": "9"
},
{
"name": "10",
"value": "10"
},
{
"name": "11",
"value": "11"
},
{
"name": "12",
"value": "12"
},
{
"name": "13",
"value": "13"
},
{
"name": "14",
"value": "14"
},
{
"name": "15",
"value": "15"
},
{
"name": "16",
"value": "16"
},
{
"name": "17",
"value": "17"
},
{
"name": "18",
"value": "18"
},
{
"name": "19",
"value": "19"
},
{
"name": "20",
"value": "20"
},
{
"name": "21",
"value": "21"
},
{
"name": "22",
"value": "22"
},
{
"name": "X",
"value": "X"
},
{
"name": "Y",
"value": "Y"
},
{
"name": "reserved loci",
"value": "reserved"
},
{
"name": "mitochondrial",
"value": "mito"
},
{
"name": "pseudoautosomal",
"value": "XandY"
}
]
},
"STATUSES": [
{
"name": "Approved",
"value": "Approved"
},
{
"name": "Entry and symbol withdrawn",
"value": "Entry Withdrawn"
}
],
"ATTRIBUTES": {
"HGNC": [
{
"name": "HGNC ID",
"value": "gd_hgnc_id"
},
{
"name": "Approved symbol",
"value": "gd_app_sym"
},
{
"name": "Approved name",
"value": "gd_app_name"
},
{
"name": "Status",
"value": "gd_status"
},
{
"name": "Locus type",
"value": "gd_locus_type"
},
{
"name": "Locus group",
"value": "gd_locus_group"
},
{
"name": "Previous symbols",
"value": "gd_prev_sym"
},
{
"name": "Previous name",
"value": "gd_prev_name"
},
{
"name": "Synonyms",
"value": "gd_aliases"
},
{
"name": "Name synonyms",
"value": "gd_name_aliases"
},
{
"name": "Chromosome",
"value": "gd_pub_chrom_map"
},
{
"name": "Date approved",
"value": "gd_date2app_or_res"
},
{
"name": "Date modified",
"value": "gd_date_mod"
},
{
"name": "Date symbol changed",
"value": "gd_date_sym_change"
},
{
"name": "Date name changed",
"value": "gd_date_name_change"
},
{
"name": "Accession numbers",
"value": "gd_pub_acc_ids"
},
{
"name": "Enzyme IDs",
"value": "gd_enz_ids"
},
{
"name": "NCBI Gene ID",
"value": "gd_pub_eg_id"
},
{
"name": "Ensembl gene ID",
"value": "gd_pub_ensembl_id"
},
{
"name": "Mouse genome database ID",
"value": "gd_mgd_id"
},
{
"name": "Specialist database links",
"value": "gd_other_ids"
},
{
"name": "Specialist database IDs",
"value": "gd_other_ids_list"
},
{
"name": "Pubmed IDs",
"value": "gd_pubmed_ids"
},
{
"name": "RefSeq IDs",
"value": "gd_pub_refseq_ids"
},
{
"name": "Gene group ID",
"value": "family.id"
},
{
"name": "Gene group name",
"value": "family.name"
},
{
"name": "CCDS IDs",
"value": "gd_ccds_ids"
},
{
"name": "Vega IDs",
"value": "gd_vega_ids"
},
{
"name": "Locus specific databases",
"value": "gd_lsdb_links"
}
],
"EXTERNAL": [
{
"name": "NCBI Gene ID",
"source": "NCBI",
"value": "md_eg_id"
},
{
"name": "OMIM ID",
"source": "OMIM",
"value": "md_mim_id"
},
{
"name": "RefSeq",
"source": "NCBI",
"value": "md_refseq_id"
},
{
"name": "UniProt ID",
"source": "UniProt",
"value": "md_prot_id"
},
{
"name": "Ensembl ID",
"source": "Ensembl",
"value": "md_ensembl_id"
},
{
"name": "Vega ID",
"source": "Vega",
"value": "md_vega_id"
},
{
"name": "UCSC ID",
"source": "UCSC",
"value": "md_ucsc_id"
},
{
"name": "Mouse genome database ID",
"source": "MGI",
"value": "md_mgd_id"
},
{
"name": "Rat genome database ID",
"source": "RGD",
"value": "md_rgd_id"
},
{
"name": "LNCipedia",
"source": "LNCipedia",
"value": "md_lncipedia"
},
{
"name": "GtRNAdb",
"source": "GtRNAdb",
"value": "md_gtrnadb"
}
]
},
"ORDER_BY": [
{
"name": "HGNC ID",
"value": "gd_hgnc_id"
},
{
"name": "Approved symbol",
"value": "gd_app_sym_sort"
},
{
"name": "Approved name",
"value": "gd_app_name"
},
{
"name": "Status",
"value": "gd_status"
},
{
"name": "Locus type",
"value": "gd_locus_type"
},
{
"name": "Locus group",
"value": "gd_locus_group"
},
{
"name": "Previous symbols",
"value": "gd_prev_sym"
},
{
"name": "Previous name",
"value": "gd_prev_name"
},
{
"name": "Synonyms",
"value": "gd_aliases"
},
{
"name": "Name synonyms",
"value": "gd_name_aliases"
},
{
"name": "Chromosome",
"value": "gd_pub_chrom_map_sort"
},
{
"name": "Date approved",
"value": "gd_date2app_or_res"
},
{
"name": "Date modified",
"value": "gd_date_mod"
},
{
"name": "Date symbol changed",
"value": "gd_date_sym_change"
},
{
"name": "Date name changed",
"value": "gd_date_name_change"
},
{
"name": "Accession numbers",
"value": "gd_pub_acc_ids"
},
{
"name": "Enzyme IDs",
"value": "gd_enz_ids"
},
{
"name": "NCBI Gene ID",
"value": "gd_pub_eg_id"
},
{
"name": "Ensembl gene ID",
"value": "gd_pub_ensembl_id"
},
{
"name": "Mouse genome database ID",
"value": "gd_mgd_id"
},
{
"name": "Specialist database links",
"value": "gd_other_ids"
},
{
"name": "Specialist database IDs",
"value": "gd_other_ids_list"
},
{
"name": "Pubmed IDs",
"value": "gd_pubmed_ids"
},
{
"name": "RefSeq IDs",
"value": "gd_pub_refseq_ids"
},
{
"name": "Gene group ID",
"value": "family.id"
},
{
"name": "Gene group name",
"value": "family.name"
},
{
"name": "CCDS IDs",
"value": "gd_ccds_ids"
},
{
"name": "Vega IDs",
"value": "gd_vega_ids"
},
{
"name": "Locus specific databases",
"value": "gd_lsdb_links"
},
{
"name": "NCBI Gene ID (supplied by NCBI)",
"value": "md_eg_id"
},
{
"name": "OMIM ID (supplied by OMIM)",
"value": "md_mim_id"
},
{
"name": "RefSeq (supplied by NCBI)",
"value": "md_refseq_id"
},
{
"name": "UniProt ID (supplied by UniProt)",
"value": "md_prot_id"
},
{
"name": "Ensembl ID (supplied by Ensembl)",
"value": "md_ensembl_id"
},
{
"name": "Vega ID (supplied by Vega)",
"value": "md_vega_id"
},
{
"name": "UCSC ID (supplied by UCSC)",
"value": "md_ucsc_id"
},
{
"name": "Mouse genome database ID (supplied by MGI)",
"value": "md_mgd_id"
},
{
"name": "Rat genome database ID (supplied by RGD)",
"value": "md_rgd_id"
},
{
"name": "LNCipedia ID (supplied by LNCipedia)",
"value": "md_lncipedia"
},
{
"name": "GtRNAdb ID (supplied by GtRNAdb)",
"value": "md_gtrnadb"
}
],
"OUTPUT": [
"Text",
"Make URL for text"
]
}
I'd like a smart way to extract the path info of this tree structure.
paths is your friend.
Given certain irregularities in the input, the exact requirements are
not always clear, but the following might be what you are looking for
and even if not, it would be easy to tweak in accordance with your
detailed requirements.
totsv.jq
def s: map(select(type=="string"));
paths as $p
| getpath($p)
| if type == "object" and has("name")
then ($p|s) + [.name, .value, (.source // empty)]
elif type == "array" and .[0] == "Text" then ($p|s) + .
else empty
end
| #tsv
Invocation
jq -crf totsv.jq chromosomes.json
Selection from output
CHROMOSOMES HUMAN 1 1
CHROMOSOMES HUMAN 2 2
...
STATUSES Approved Approved
STATUSES Entry and symbol withdrawn Entry Withdrawn
ATTRIBUTES HGNC HGNC ID gd_hgnc_id
...
ORDER_BY GtRNAdb ID (supplied by GtRNAdb) md_gtrnadb
OUTPUT Text Make URL for text
For future reference
Rather than give a very long sample input, it would be better
to give a small sample that is tightly woven with detailed requirements.

Json file editing through jq

I have json
{
"file1": [{
"username": "myname",
"groupname": "mypassword",
"environment": [{
"name": "UMASK",
"value": "022"
},
{
"name": "DEBUG",
"value": "2"
}]
}]
}
and want to change the value of DEBUG to 5.
Tried with below command
jq .file1[0].environment sandeep.json |jq '(.[] |select(.name ==
"DEBUG") | .value) |= "5"'
this will return me specific portion of json like
[
{
"name": "UMASK",
"value": "022"
},
{
"name": "DEBUG",
"value": "5"
}
]
but I want to see full json with changed value
{
"file1": [{
"username": "myname",
"groupname": "mypassword",
"environment": [{
"name": "UMASK",
"value": "022"
},
{
"name": "DEBUG",
"value": "5"
}]
}]
}
Please suggest me
It should be:
jq '(.file1[].environment[]|select(.name=="DEBUG").value) |= 5' file.json
Output:
{
"file1": [
{
"username": "myname",
"groupname": "mypassword",
"environment": [
{
"name": "UMASK",
"value": "022"
},
{
"name": "DEBUG",
"value": 5
}
]
}
]
}