对于以下JSON文件,我很难生成命令

发布于 2025-02-11 16:39:51 字数 1401 浏览 0 评论 0原文

这就是我的JSON文件的外观,

   "Patient":{
      "Name":{
         "Patient First Name":"James",
         "Patient Middle Name":"Adam",
         "Patient Last Name":"Manchester"
      },
      "Unique ID":163983,
      "Demographics":{
         "Sex":"M",
         "Birthdate":"7/24/1940"
      },
      "IntakeCriteria":{
         "DmDxDate":"8/25/2012",
         "InitialHgbA1c":8.1,
         "Comorbidity":["Diabetes Mellitus","CHF","DVT"]
      },
      "Labs":{
         "LDLCholesterol":{
            "LDLResultDate":"5/6/2013",
            "LDLLevel":200
         },
         "SerumCreatinine":{
            "CreatinineResultDate":"11/1/2016",
            "CreatinineLevel":0.9
         }
      },
      "CareLocation":{
         "Facility Name":"East Side Clinic",
         "Facility Contact Name":"Mary Silverman",
         "Facility Contact Phone":"618-348-1891"
      }
   }
}

我需要使用以下字段来准备查询:

Patient Unique ID number.
Patient First Name.
Patient Last Name.
Lab Test Date.
HgbA1c Level.
LDL Level.
Creatinine Level.

我尝试了此结果,因此没有结果

jq - r’[.Patient.UniqueID, .Patient.Name.FirstName, .Patient.Name.LastName, .Patient.Labs.LDLCholesterol.LabResultDate, .Patient.IntakeCriteria.InitialHgbA1c, .Patient.Labs.LDLCholesterol.LDLLevel, .Patient.Labs.SerumCreatine.CreatineLevel] | @csv’ data.json > csvtest.out

This is how my json file looks

   "Patient":{
      "Name":{
         "Patient First Name":"James",
         "Patient Middle Name":"Adam",
         "Patient Last Name":"Manchester"
      },
      "Unique ID":163983,
      "Demographics":{
         "Sex":"M",
         "Birthdate":"7/24/1940"
      },
      "IntakeCriteria":{
         "DmDxDate":"8/25/2012",
         "InitialHgbA1c":8.1,
         "Comorbidity":["Diabetes Mellitus","CHF","DVT"]
      },
      "Labs":{
         "LDLCholesterol":{
            "LDLResultDate":"5/6/2013",
            "LDLLevel":200
         },
         "SerumCreatinine":{
            "CreatinineResultDate":"11/1/2016",
            "CreatinineLevel":0.9
         }
      },
      "CareLocation":{
         "Facility Name":"East Side Clinic",
         "Facility Contact Name":"Mary Silverman",
         "Facility Contact Phone":"618-348-1891"
      }
   }
}

I need to prepare a query using the following fields:

Patient Unique ID number.
Patient First Name.
Patient Last Name.
Lab Test Date.
HgbA1c Level.
LDL Level.
Creatinine Level.

I tried this resulting in no result

jq - r’[.Patient.UniqueID, .Patient.Name.FirstName, .Patient.Name.LastName, .Patient.Labs.LDLCholesterol.LabResultDate, .Patient.IntakeCriteria.InitialHgbA1c, .Patient.Labs.LDLCholesterol.LDLLevel, .Patient.Labs.SerumCreatine.CreatineLevel] | @csv’ data.json > csvtest.out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2025-02-18 16:39:51

当使用您必须按照JSON文件中的使用完全提供字段名称。另外,如果它们包含特殊字符,例如白色空间(或以数字开头),请用双引号包围以澄清:

jq -r '[
  .Patient."Unique ID",
  .Patient.Name."Patient First Name",
  .Patient.Name."Patient Last Name",
  .Patient.Labs.LDLCholesterol.LDLResultDate,
  .Patient.IntakeCriteria.InitialHgbA1c,
  .Patient.Labs.LDLCholesterol.LDLLevel,
  .Patient.Labs.SerumCreatinine.CreatinineLevel
] | @csv' data.json > csvtest.out
163983,"James","Manchester","5/6/2013",8.1,200,0.9

demo

要稍微缩短过滤器,您也可以使用带管的子滤光器 |与括号进行分组 ()

jq -r '[.Patient |
  ."Unique ID",
  (.Name | ."Patient First Name", ."Patient Last Name"),
  .Labs.LDLCholesterol.LDLResultDate,
  .IntakeCriteria.InitialHgbA1c,
  (.Labs | .LDLCholesterol.LDLLevel, .SerumCreatinine.CreatinineLevel)
] | @csv' data.json > csvtest.out
163983,"James","Manchester","5/6/2013",8.1,200,0.9

When using object identifiers, you have to provide the field names exactly as used in the JSON file. Also, if they contain special characters such as white spaces (or start with a digit), surround them with double quotes for clarification:

jq -r '[
  .Patient."Unique ID",
  .Patient.Name."Patient First Name",
  .Patient.Name."Patient Last Name",
  .Patient.Labs.LDLCholesterol.LDLResultDate,
  .Patient.IntakeCriteria.InitialHgbA1c,
  .Patient.Labs.LDLCholesterol.LDLLevel,
  .Patient.Labs.SerumCreatinine.CreatinineLevel
] | @csv' data.json > csvtest.out
163983,"James","Manchester","5/6/2013",8.1,200,0.9

Demo

To shorten your filter a bit, you may also group together some of the paths sharing common parents using subfilters with pipes | within a grouping with parentheses ():

jq -r '[.Patient |
  ."Unique ID",
  (.Name | ."Patient First Name", ."Patient Last Name"),
  .Labs.LDLCholesterol.LDLResultDate,
  .IntakeCriteria.InitialHgbA1c,
  (.Labs | .LDLCholesterol.LDLLevel, .SerumCreatinine.CreatinineLevel)
] | @csv' data.json > csvtest.out
163983,"James","Manchester","5/6/2013",8.1,200,0.9

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文