使用JQ替换JSON中的下划线 [英] Replacing underscores in JSON using JQ

查看:260
本文介绍了使用JQ替换JSON中的下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 woocommerce API进行检索和存储信息.当前,我们的设置被设计为使用驼峰大小写而不是下划线.我正在使用jq处理我们的信息,但是我很好奇如何使用sub(regex, tostring)函数用camelCase替换JSON中的下划线?

I'm working with the woocommerce API to retrieve and store information. Currently our setup is designed to use camel case instead of underscores. I'm using jq to process our information, but I'm curious how I can use the sub(regex, tostring) function to replace the underscores in my JSON with camelCase?

这是代码示例

"line_items": [
    {
     "id": xxxx,
     "name": "xxxx",
     "sku": "xxxx",
     "product_id": xxxx,
    }

例如,根据我发现的关于SO的另一个答案,此方法有效:curl https://www.testsite.com/wp-json/wc/v1/orders -u user:pass | jq '.[] | with_entries( if .key | contains("_") then .key |= sub("_";"") else . end)'并删除下划线.

For example, according to another answer on SO that I found, this works: curl https://www.testsite.com/wp-json/wc/v1/orders -u user:pass | jq '.[] | with_entries( if .key | contains("_") then .key |= sub("_";"") else . end)' and remove the underscores.

结果是:

"lineitems": [
    {
     "id": xxxx,
     "name": "xxxx",
     "sku": "xxxx",
     "productid": xxxx,
    }

但是,当我尝试curl https://www.testsite.com/wp-json/wc/v1/orders -u user:pass | jq '.[] | with_entries( if .key | contains("_") then .key |= sub("(\\_)([a-z])";"$2\u") else . end)'时,却没有得到我期望的结果.

However, when I try curl https://www.testsite.com/wp-json/wc/v1/orders -u user:pass | jq '.[] | with_entries( if .key | contains("_") then .key |= sub("(\\_)([a-z])";"$2\u") else . end)' I don't get the results I would expect.

预期结果将是:

"lineItems": [
    {
     "id": xxxx,
     "name": "xxxx",
     "sku": "xxxx",
     "productId": xxxx,
    }

我没有很多使用jq的经验,所以我不确定自己做错了什么.有没有更好的解决方案来解决这个问题?

I don't have a lot of experience using jq so I'm not sure what I'm doing wrong. Is there a better solution to this problem?

推荐答案

这是一个jq函数,它将"a_bcd_ef"转换为"aBcdEf",这似乎就是您想要的:

Here's a jq function that will convert "a_bcd_ef" to "aBcdEf", which seems to be what you want:

def camel:
  gsub( "_(?<a>[a-z])"; .a|ascii_upcase);

示例用法:

"a_bcd_ef" | camel

如果您想要一个简单的单行代码来处理来自STDIN的JSON字符串:

If you want a simple one-liner to process JSON strings from STDIN:

$ jq 'gsub( "_(?<a>[a-z])"; .a|ascii_upcase)'

如果只想转换"_ [a-z]"的第一个出现,那么您当然会使用sub.依此类推.

If you only want the first occurrence of "_[a-z]" converted, then of course you'd use sub. And so on.

要将此功能应用于对象中的所有键,您可以编写:

To apply this function to ALL keys in an object, you could write:

with_entries( .key |= camel )

要更改JSON实体中所有对象中的所有键,可以使用walk/1:

To change ALL keys in ALL objects within a JSON entity, you could use walk/1:

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

如果您的jq没有walk/1,则可以在调用它之前或在您的〜/.jq文件中简单地包括它的定义(可通过谷歌搜索轻松找到).

If your jq does not have walk/1 then you can simply include its definition (easily found by googling), either before it is invoked, or perhaps in your ~/.jq file.

这篇关于使用JQ替换JSON中的下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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