无法使用jq捕获元素的值

时间:2019-07-11 11:40:11

标签: json jq

我正在尝试使用jq提取dumpName的内容,但是它不起作用。我试图简化原始文件的结构,因为我无法在此处发布完整的详细信息。

我也想获取param2的内容。如果需要,可以将其收集在单独的jq语句中。

{
  "get": {
    "download": {
      "ebR": true,
      "detail": [
        {
          "downloadHost": "xxx.jgs.com",
          "downloadURL": "xxx.jgs.com",
          "dumpInfo": {
            "dumpCategory": "Other",
            "dumpContentType": "UNKNOWN",
            "dumpId": test,
            "dumpName": "name_of_dump",
            "dumpSize": 1200,
            "dumpStatus": "COMPLETED",
            "dumpUploadDate": "date",
            "vis": "who_see_it"
          },
          "datastore": [
            {
              "param1": "random_val",
              "param2": "random_val"
            },
            {
              "param1": "testtest",
              "param2": "testtest"
            }
          ],
          "Info": {
            "webpage": "test@test.com"
          }
        }
  }
}
}

所需的输出:

name_of_dump
random_val
testtest

以前的尝试:

jq '.[] | .[]'   # gives section from { ebR
jq '.[] | .[] | .[]'   # gives error:

是 jq:错误(处:399):无法遍历字符串(“ http://www..。)

jq '.[] | .[] | .[] | .dumpName'  # gives error:

jq:错误(处:399):无法使用字符串“ dumpName”索引布尔值

谢谢。

4 个答案:

答案 0 :(得分:2)

您需要使用正确的对象/数组名称递归JSON。使用普通的{Private Declare PtrSafe Function OpenProcess Lib "kernel32" _ (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" _ (ByVal hProcess As Long, lpExitCode As Long) As Long Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Const STILL_ACTIVE = &H103 Const PROCESS_QUERY_INFORMATION = &H400 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell("cmd.exe /C " & ThisWorkbook.Path & "\MacroPolosSalientes.vbs")) Do 'Get the status of the process GetExitCodeProcess hProcess, RetVal 'Sleep command recommended as well DoEvents 'Loop while the process is active Loop While RetVal = STILL_ACTIVE} 表示法将无法实现

.[]

输入的JSON在语法上也不合法。参见此jqplay URL,其中显示了过滤器在运行时正常工作。

答案 1 :(得分:1)

您可能想考虑一下这种单线:

jq '.. | (.dumpName? // empty), (.param2? // empty)' input.json

答案 2 :(得分:0)

由于用户要求显示 dumpName param2 ,因此以下是正确的代码:

jq -r '.get.download.detail[] | .dumpInfo.dumpName, .datastore[].param2'

输出:

name_of_dump
random_val
testtest

作为上述用户(印度语),您的JSON文件无效。

这是有效的JSON:

{
  "get": {
    "download": {
      "ebR": true,
      "detail": [
        {
          "downloadHost": "xxx.jgs.com",
          "downloadURL": "xxx.jgs.com",
          "dumpInfo": {
            "dumpCategory": "Other",
            "dumpContentType": "UNKNOWN",
            "dumpId": "test",
            "dumpName": "name_of_dump",
            "dumpSize": 1200,
            "dumpStatus": "COMPLETED",
            "dumpUploadDate": "date",
            "vis": "who_see_it"
          },
          "datastore": [
            {
              "param1": "random_val",
              "param2": "random_val"
            },
            {
              "param1": "testtest",
              "param2": "testtest"
            }
          ],
          "Info": {
            "webpage": "test@test.com"
          }
        }
      ]
    }
  }
}

答案 3 :(得分:0)

另外,显示JSON内容的替代简便方法是向我们提供基于步行路径的Unix实用程序 jtc

bash $ <download.json jtc -qq -w'<dumpName>l' -w'<param2>l:'
name_of_dump
random_val
testtest
bash $ 

PS>披露:我是jtc-用于JSON操作的shell cli工具的创建者