Replacing underscores in JSON using JQ

╄→гoц情女王★ 提交于 2019-12-10 17:46:50

问题


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?

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.

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.

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?


回答1:


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);

Example usage:

"a_bcd_ef" | camel

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

$ 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.

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:

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.



来源:https://stackoverflow.com/questions/40366520/replacing-underscores-in-json-using-jq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!