使用JQ替换JSON中的下划线

时间:2021-12-23 16:51:53

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?

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

Here's an example of the code

这里有一个代码示例

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

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.

例如,根据我发现的另一个答案,这是可行的:curl https://www.testsite.com/wp-json/wc/v1/orders -u用户:传递| jq '。[]| with_entries(如果。key |包含(“_”),那么。key | =子(“_”;" ")。并删除下划线。

The result is:

其结果是:

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

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.

然而,当我尝试curl https://www.testsite.com/wp-json/wc/v1/orders -u时:传递| jq '。[]| with_entries(如果。key |包含(“_”),那么。key | =子(“(\ \ _)([a - z])”;“2美元\ u”)。我没有得到我期望的结果。

The expected results would be:

预期的结果将是:

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

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的经验,所以我不确定我做错了什么。这个问题有更好的解决办法吗?

1 个解决方案

#1


3  

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

这里有一个将“a_bcd_ef”转换为“aBcdEf”的jq函数,这似乎是您想要的:

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

Example usage:

使用示例:

"a_bcd_ef" | camel

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

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

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

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

如果你只希望第一个出现的“_[a-z]”被转换,那么你当然会使用sub.等等。

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

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

with_entries( .key |= camel )

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

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

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

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没有walk/1,那么您可以简单地包含它的定义(通过google搜索很容易找到),或者在调用它之前,或者可能在~/中。金桥文件。

#1


3  

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

这里有一个将“a_bcd_ef”转换为“aBcdEf”的jq函数,这似乎是您想要的:

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

Example usage:

使用示例:

"a_bcd_ef" | camel

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

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

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

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

如果你只希望第一个出现的“_[a-z]”被转换,那么你当然会使用sub.等等。

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

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

with_entries( .key |= camel )

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

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

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

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没有walk/1,那么您可以简单地包含它的定义(通过google搜索很容易找到),或者在调用它之前,或者可能在~/中。金桥文件。