You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jq访问不存在的数组成员时返回默认null的实现问题求助

问题描述

处理以下两个JSON对象时遇到报错:

{
   "generalPractitioner":[
      {
         "extension":[
            {
               "url":"http://catsalut.gencat.cat/fhir/StructureDefinition/patient-tipus-relacio",
               "valueCodeableConcept":{
                  "coding":[
                     {
                        "system":"http://catsalut.gencat.cat/fhir/StructureDefinition/tipus-rel-organitzacio",
                        "code":"UPAPASSIG"
                     }
                  ]
               }
            }
         ],
         "reference":"Organization/132336"
      },
      {
         "extension":[
            {
               "url":"http://catsalut.gencat.cat/fhir/StructureDefinition/patient-tipus-relacio",
               "valueCodeableConcept":{
                  "coding":[
                     {
                        "system":"http://catsalut.gencat.cat/fhir/StructureDefinition/tipus-rel-organitzacio",
                        "code":"UPAPTERRI"
                     }
                  ]
               }
            }
         ],
         "reference":"Organization/132346"
      }
   ]
}
{
   "generalPractitioner":[
      {
         "extension":[
            {
               "url":"http://catsalut.gencat.cat/fhir/StructureDefinition/patient-tipus-relacio",
               "valueCodeableConcept":{
                  "coding":[
                     {
                        "system":"http://catsalut.gencat.cat/fhir/StructureDefinition/tipus-rel-organitzacio",
                        "code":"UPAPASSIG"
                     }
                  ]
               }
            }
         ],
         "reference":"Organization/132336"
      }
   ]
}
现有实现与报错

当前使用的jq命令如下:

jq --raw-output '[ ( .generalPractitioner[0] | ( .extension[] | .url, ( .valueCodeableConcept | .coding[] | .system, .code ) ), .reference ), ( .generalPractitioner[1] | ( .extension[] | .url, ( .valueCodeableConcept | .coding[] | .system, .code ) ), .reference ) ] | @csv'

整理后的过滤表达式:

[
  ( .generalPractitioner[0] |
    ( .extension[] |
        .url,
        ( .valueCodeableConcept |
          .coding[] |
            .system,
            .code
        )
     ),
     .reference
  ),
    ( .generalPractitioner[1] |
    ( .extension[] |
        .url,
        ( .valueCodeableConcept |
          .coding[] |
            .system,
            .code
        )
     ),
     .reference
  ),
] | @csv

执行返回报错:

jq: error (at :55): Cannot iterate over null (null)

报错原因是第二个JSON对象的generalPractitioner数组仅包含1个元素,直接访问.generalPractitioner[1]触发了空值访问错误。

需求说明

访问.generalPractitioner[N](N取0、1、2、3)对应索引不存在时,对应的.extension[0].url、.extension[0].valueCodeableConcept.coding[0].system、.extension[0].valueCodeableConcept.coding[0].code、.reference四个字段均返回默认null值,最终将所有字段输出为csv格式。

解决方案

修改后的jq命令如下:

jq --raw-output '
[range(0;4) as $idx
 | .generalPractitioner[$idx] // {}
 | (
     .extension[0].url,
     .extension[0].valueCodeableConcept.coding[0].system,
     .extension[0].valueCodeableConcept.coding[0].code,
     .reference
   ) // null
]
| @csv
'

修改说明

  • 用range(0;4)自动遍历0~3四个下标,无需手动硬编码每个下标的取值逻辑
  • 用//运算符做缺省值兜底,当数组下标不存在/字段不存在时自动返回null,不会触发迭代空值的报错
  • 统一用.extension[0]、.coding[0]取首个元素,避免用[]迭代多个元素导致字段错位,匹配需求中取首个扩展、首个编码的要求

内容的提问来源于stack exchange,提问作者Jordi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 04:57:00