更新字段时已知 jq 错误的解决方法 [英] workaround for known jq error while updating fields

查看:30
本文介绍了更新字段时已知 jq 错误的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新字符串"类型的每个值如果可能的话,将整个 json 转换为一个数字.如果该值无法解析为数字,则应保留该字符串.

I want to update each value of type "string" in an entire json to a number if possible. If the value cannot be parsed as a number, the string should be kept.

我用 jq 解决这个问题的尝试遇到了 1.6 版的已知错误.

My attempt to solve this with jq runs into a known bug of version 1.6.

我不知道如何解决这个错误.任何想法如何在不遇到此已知错误的情况下解决任务?

I can't figure out how to workaround this bug. Any ideas how to solve the task without running into this known bug?

另见:更新运算符在 JQ 中产生空对象

{
  "outer": {
    "inner": [
      {
        "foo": "foo",
        "bar": "11",
        "count": 12,
        "data": ["13", "14"]
      },
      {
        "foo": "foo",
        "bar": "15",
        "count": 16,
        "child": {
          "otherData": ["17", "18"]
        }
      }
    ]
  }
}

期望的输出

{
  "outer": {
    "inner": [
      {
        "foo": "foo",
        "bar": 11,
        "count": 12,
        "data": [13, 14]
      },
      {
        "foo": "foo",
        "bar": 15,
        "count": 16,
        "child": {
          "otherData": [17, 18]
        }
      }
    ]
  }
}


尝试 1 解决 - 不起作用

jq '(..| strings) |= try to number catch .'

输出

{
  "outer": {
    "inner": [
      {
        "foo": "Invalid literal at EOF at line 1, column 3 (while parsing 'foo')",
        "bar": {
          "__jq": 1
        },
        "count": 12,
        "data": [
          {
            "__jq": 2
          },
          {
            "__jq": 3
          }
        ]
      },
      {
        "foo": "Invalid literal at EOF at line 1, column 3 (while parsing 'foo')",
        "bar": {
          "__jq": 5
        },
        "count": 16,
        "child": {
          "otherData": [
            {
              "__jq": 6
            },
            {
              "__jq": 7
            }
          ]
        }
      }
    ]
  }
}

尝试 2 解决 - 更糟

jq '(..| strings) |= tonumber?//.'

输出

{
  "outer": {
    "inner": [
      {
        "count": 12,
        "data": [
          "14"
        ]
      },
      {
        "count": 16,
        "child": {
          "otherData": [
            "18"
          ]
        }
      }
    ]
  }
}

按预期工作 - 但没有解决任务

bug 与 update 操作符结合 try catch 表达式有关

working as expected - but does not solve the task

the bug is related to update operator in combination with try catch expression

jq '(..| 字符串) |= (. + "___needs_update")'

{
  "outer": {
    "inner": [
      {
        "foo": "foo___needs_update",
        "bar": "11___needs_update",
        "count": 12,
        "data": [
          "13___needs_update",
          "14___needs_update"
        ]
      },
      {
        "foo": "foo___needs_update",
        "bar": "15___needs_update",
        "count": 16,
        "child": {
          "otherData": [
            "17___needs_update",
            "18___needs_update"
          ]
        }
      }
    ]
  }
}

推荐答案

walk 去救援:

walk(if type == "string"
     then . as $in | try tonumber catch $in 
     else . end)

或者只是:

walk(if type == "string" then tonumber? // . else . end)

增强步行

如果您的 jq 没有 walk,或者如果您想要相对于(当前)内置版本的增强版本,请参阅 jq FAQ(搜索def walk).

Enhanced walk

If your jq does not have walk, or if you want an enhanced version relative to the (current) built-in version, see the jq FAQ (search for def walk).

这篇关于更新字段时已知 jq 错误的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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