【问题标题】:jq assignment where the path contain space路径包含空格的 jq 赋值
【发布时间】:2018-01-31 07:40:34
【问题描述】:

这可能是一个简单的问题,但我正在努力弄清楚 jq 如何处理元素中的空格。 (以下是windows powershell)

PS C:\Users\Home> "{}" | jq ".mytest.path = """"success""""" 
{
  "mytest": {
    "path": "success"
  }
}

现在,我想让“我的测试”成为两个单独的词。我想要的输出是

{
  "my - test": {
    "path": "success"
  }
}

我会使用什么查询?我尝试了以下方法:

PS C:\Users\Home> "{}" | jq ".""my - test"".path = """"success""""" 
jq : jq: error: Could not open file test.path = "success": Invalid argument
At line:1 char:8
+ "{}" | jq ".""my - test"".path = """"success"""""
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (jq: error: Coul...nvalid argument:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PS C:\Users\Home> "{}" | jq --raw-output ".""my test"".path = """"success""""" 
jq : jq: error: Could not open file test.path = "success": Invalid argument
At line:1 char:8
+ "{}" | jq --raw-output ".""my test"".path = """"success"""""
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (jq: error: Coul...nvalid argument:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

有什么建议吗?

【问题讨论】:

    标签: windows powershell syntax key jq


    【解决方案1】:

    首先,抛开所有与不同shell特性相关的问题,你需要的jq过滤器是:

    .["my test"].path = "success"
    

    (如果对使用 .foo 语法有疑问,您可以随时使用基本形式:.["key name"]。)

    由于您显然熟悉强大的 PowerShell 所需的变形,所以我在这里只指出,可以简单地将 jq 程序放在一个文件中(比如 program.jq)并使用 -f 选项调用 jq ( jq -f program.jq ...)。

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 2011-05-19
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多