使用jq将所有json键转换为小写 [英] Convert all json keys to lowercase using jq
问题描述
我希望将包含数组的JSON文件提取到数据库中。具有数组项的json文件如下:-
I'm looking to ingest a JSON file with arrays into my database. The json file with array items is as below:-
{
"campaignId": "11067182",
"campaignName": "11067182",
"channelId": "%pxbid_universal_site_id=!;",
"channelName": "%pxbid_universal_site_id=!;",
"placementId": "%epid!",
"placementName": "%epid!",
"publisherId": "%esid!",
"publisherName": "%esid!",
"hitDate": "2017-03-23",
"lowRiskImpressions": "61485",
"lowRiskPct": "64.5295",
"moderateRiskImpressions": "1887",
"moderateRiskPct": "1.9804",
"highRiskImpressions": "43",
"highRiskPct": "0.0451",
"veryHighRiskImpressions": "860",
"veryHighRiskPct": "0.9026",
"totalRated": "95274",
"unrated": "8",
"unratedPct": "0.0084",
"visibleCount": "64283",
"pctVisible": "67.4660",
"invisibleCount": "30999",
"totalImpressions": "95282"
}
{
"campaignId": "11067182",
"campaignName": "11067182",
"channelId": "%pxbid_universal_site_id=!;",
"channelName": "%pxbid_universal_site_id=!;",
"placementId": "%epid!",
"placementName": "%epid!",
"publisherId": "%esid!",
"publisherName": "%esid!",
"hitDate": "2017-03-22",
"lowRiskImpressions": "17929",
"lowRiskPct": "52.9379",
"moderateRiskImpressions": "1872",
"moderateRiskPct": "5.5273",
"highRiskImpressions": "9",
"highRiskPct": "0.0266",
"veryHighRiskImpressions": "139",
"veryHighRiskPct": "0.4104",
"totalRated": "33850",
"unrated": "18",
"unratedPct": "0.0531",
"visibleCount": "19967",
"pctVisible": "58.9554",
"invisibleCount": "13901",
"totalImpressions": "33868"
}
是否可以重现相同的数组,但是使用 jq $ c $将键转换为小写c>?
Is there a way to reproduce the same array, but with the keys converted to lowercase using jq
?
推荐答案
简短的回答是,
在提到的特定情况下,可以使用以下过滤器:
In the particular case you mention, you could use this filter:
with_entries( .key |= ascii_downcase )
但是,你提到数组。如果要引用JSON数组,则必须修改上面的内容。
However, you mention arrays. If you are referring to JSON arrays, you will have to modify the above.
一种可能是使用 walk / 1
,但您的jq可能没有。如果要使用 walk / 1
,可以通过以下方式在jq中找到其定义:jq def walk
One possibility would be to use walk/1
, but your jq might not have it. If you want to use walk/1
, you could find its definition in jq by googling for: jq def walk
使用方法如下:
walk(if type=="object" then with_entries(.key|=ascii_downcase) else . end)
这篇关于使用jq将所有json键转换为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!