duyCuong
duyCuong

Reputation: 57

Replace a character with another in object keys

I am learning how to use jq to manipulate json data,  I'm having a little trouble with it. This is my input JSON

{
   "user":{
      "advertisingID":"617a68"
   },
   "deviceTs":1575387020137,
   "activies":[
       {
         "ts":1575617868326,
         "appsUsage":{
            "isFull":true,
            "data":[
               {
                  "com.orange.phone":44009
               }
            ],
            "startTs":1575617281541
         }
      },
      {
         "ts":1575618968326,
         "appsUsage":{
            "isFull":true,
            "data":[
               {
                  "uk.green.launcher2":4354
               },
               {
                  "com.black.phone":1232
               }
            ],
            "startTs":1575617281541
         }
      }
   ]
}

I want to replace all keys containing "dots" by "dashes" and expected output:

{
   "user":{
      "advertisingID":"617a68"
   },
   "deviceTs":1575387020137,
   "activies":[
       {
         "ts":1575617868326,
         "appsUsage":{
            "isFull":true,
            "data":[
               {
                  "com-orange-phone":44009    <----
               }
            ],
            "startTs":1575617281541
         }
      },
      {
         "ts":1575618968326,
         "appsUsage":{
            "isFull":true,
            "data":[
               {
                  "uk-green-launcher2":4354   <----
               },
               {
                  "com-black-phone":1232      <----
               }
            ],
            "startTs":1575617281541
         }
      }
   ]
}

I have tried with .activies |= map( with_entries(if .key == "appsUsage" then ... else . end) ) ... (split(".")|join("-")) but without success, Thanks in advance.

Upvotes: 0

Views: 114

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

I don't think you need regex for this; I believe a conjuction of split and join builtins would be more effective and clean.

.activies |= walk(
  if type == "object" then
    reduce (keys_unsorted[] | select(index("."))) as $k (.;
      (.[$k | split(".") | join("-")] = .[$k])
      | del(.[$k])
    )
  else . end
)

Online demo

Upon peak's recommendation, a more readable solution:

.activies |= walk(
  if type == "object" then
    with_entries(
      .key |= gsub("\\.";"-")
    )
  else . end
)

Online demo

Upvotes: 2

Related Questions