Convert all json keys to lowercase using JQ

Balajee Addanki picture Balajee Addanki · Mar 27, 2017 · Viewed 8.3k times · Source

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?

Answer

peak picture peak · Mar 27, 2017

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)