我正在与吴商务用于检索和存储信息的 API。目前我们的设置设计为使用驼峰式大小写而不是下划线。我在用着jq
处理我们的信息,但我很好奇如何使用sub(regex, tostring)
函数用驼峰命名法替换 JSON 中的下划线?
这是代码示例
"line_items": [
{
"id": xxxx,
"name": "xxxx",
"sku": "xxxx",
"product_id": xxxx,
}
例如,根据我发现的另一个答案,这是有效的: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,
}
然而,当我尝试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": [
{
"id": xxxx,
"name": "xxxx",
"sku": "xxxx",
"productId": xxxx,
}
我没有太多使用经验jq
所以我不确定我做错了什么。对于这个问题有更好的解决方案吗?