Convert all json keys to lowercase using jq

为君一笑 提交于 2020-07-06 10:55:01

问题


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"
}

Is there a way to reproduce the same array, but with the keys converted to lowercase using jq?


回答1:


The short answer is yes, assuming you are referring to the ASCII characters.

In the particular case you mention, you could use this filter:

with_entries( .key |= ascii_downcase )

However, you mention arrays. If you are referring to JSON arrays, you will have to modify the above.

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

Here's how it could be used:

walk(if type=="object" then with_entries(.key|=ascii_downcase) else . end)


来源:https://stackoverflow.com/questions/43038453/convert-all-json-keys-to-lowercase-using-jq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!