JQ密钥映射名称为大写并删除空间 [英] JQ key map name to uppercase and remove space

查看:54
本文介绍了JQ密钥映射名称为大写并删除空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用bash脚本,我必须使用jq自定义一个JSON obj.我有:

i'm working in a bash script i have to customize one JSON obj with jq . I have:

[
  {
    "NC dn": "DC=company,DC=org",
    "DSA objectGUID": "df9e8712-98c5-42e4-a020-2e5218404e51",
    "last attempt time": "Wed Jun 10 15:27:42 2020 CEST",
    "last attempt message": "was successful",
    "consecutive failures": 0,
    "last success": "Wed Jun 10 15:27:42 2020 CEST",
    "NTDS DN": "CN=NTDS Settings,CN=STEN0002-R2,CN=Servers,CN=TENGIZ,CN=Sites,CN=Configuration,DC=company,DC=org",
    "is deleted": false,
    "DSA": "TENGIZ\\STEN0002-R2"
  },
  {
    "NC dn": "DC=company,DC=org",
    "DSA objectGUID": "d5d168fd-5fc3-4afc-9acc-32c7f10d14a2",
    "last attempt time": "Wed Jun 10 15:27:42 2020 CEST",
    "last attempt message": "was successful",
    "consecutive failures": 0,
    "last success": "Wed Jun 10 15:27:42 2020 CEST",
    "NTDS DN": "CN=NTDS Settings,CN=SCGPN0002-R2,CN=Servers,CN=POINTE-NOIRE,CN=Sites,CN=Configuration,DC=company,DC=org",
    "is deleted": false,
    "DSA": "POINTE-NOIRE\\SCGPN0002-R2"
  }
]

但是我需要所有带有空格的键,大写的{#JSON_KEY}加上CONSECUTIVE_FAILURES值和IS_DELETED值的":

but i need all key with out space and uppercase with {#JSON_KEY} plus CONSECUTIVE_FAILURES value and IS_DELETED value with " " :

[
  {
    "{#NC_DN}": "DC=domain,DC=org",
    "{#DSA_OBJECTGUID}": "df9e8712-98c5-42e4-a020-2e5218404e51",
    "{#LAST_ATTEMPT_TIME}": "Wed Jun 10 15:27:42 2020 CEST",
    "{#LAST_ATTEMPT_MESSAGE}": "was successful",
    "{#CONSECUTIVE_FAILURES}": "0",
    "{#LAST_SUCCESS}": "Wed Jun 10 15:27:42 2020 CEST",
    "{#NTDS_DN}": "CN=NTDS Settings,CN=STEN0002-R2,CN=Servers,CN=TENGIZ,CN=Sites,CN=Configuration,DC=domain,DC=org",
    "{#IS_DELETED}": "false",
    "{#DSA}": "TENGIZ\\STEN0002-R2"
  },
  {
    "{#NC_DN}": "DC=domain,DC=org",
    "{#DSA_OBJECTGUID}": "d5d168fd-5fc3-4afc-9acc-32c7f10d14a2",
    "{#LAST_ATTEMPT_TIME}": "Wed Jun 10 15:27:42 2020 CEST",
    "{#LAST_ATTEMPT_MESSAGE}": "was successful",
    "{#CONSECUTIVE_FAILURES}": "0",
    "{#LAST_SUCCESS}": "Wed Jun 10 15:27:42 2020 CEST",
    "{#NTDS_DN}": "CN=NTDS Settings,CN=SCGPN0002,CN=Servers,CN=NOIRE,CN=Sites,CN=Configuration,DC=domain,DC=org",
    "{#IS_DELETED}": "false",
    "{#DSA}": "POINTE-NOIRE\\SCGPN0002-R2"
  }
]

预先感谢

推荐答案

以下内容满足了要求,而没有 walk 的开销:

The following meets the requirements without the overhead of walk:

map(with_entries( .key |= ( gsub( " "; "_")
                            | ascii_upcase 
                            | "{#\(.)}" )
                  | .value |= tostring))

注意事项

顾名思义, ascii_upcase 仅大写ASCII字符.

Caveat

As the name suggests, ascii_upcase only uppercases ASCII characters.

这篇关于JQ密钥映射名称为大写并删除空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆