我正在使用woocommerce API来检索和存储信息.目前我们的设置旨在使用驼峰案例而不是下划线.我正在使用jq处理我们的信息,但我很好奇如何使用该sub(regex, tostring)函数用camelCase替换我的JSON中的下划线?
这是代码的一个例子
"line_items": [
{
"id": xxxx,
"name": "xxxx",
"sku": "xxxx",
"product_id": xxxx,
}
Run Code Online (Sandbox Code Playgroud)
例如,根据我发现的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)'并删除下划线.
结果是:
"lineitems": [
{
"id": xxxx,
"name": "xxxx",
"sku": "xxxx",
"productid": xxxx,
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试时,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)'我没有得到我期望的结果.
预期结果将是:
"lineItems": …Run Code Online (Sandbox Code Playgroud)