使用JMESPath按JSON中的部分字符串匹配进行过滤

k2fxgqgv  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(115)

下面是一个JSON示例:

{
  "School": [
  {"@id": "ABC_1",
  "SchoolType": {"@tc": "10023204",
   "#text": "BLUE FOX"}},
  {"@id": "ABC_2",
  "SchoolType": {"@tc": "143", "#text": "AN EAGLE"}},
  {"@id": "ABC_3",
  "SchoolType": {"@tc": "21474836", "#text": "OTHER REASONS"},
  "SchoolStatus": {"@tc": "21474836", "#text": "FINE"},
  "Teacher": [
    {"@id": "XYZ_1",
    "TeacherType": {"@tc": "5", "#text": "GENDER"},
    "Gender": "FEMALE",
    "Extension": {"@VC": "23",
     "Extension_Teacher": {"DateDuration": {"@tc": "10023111",
       "#text": "0-6 MONTHS"}}}},
    {"@id": "XYZ_2",
    "TeacherType": {"@tc": "23", "#text": "EDUCATED"},
    "Extension": {"@VC": "23",
     "Extension_Teacher": {"DateDuration": {"@tc": "10023111",
       "#text": "CURRENT"}}}}]},
  {"@id": "ABC_4",
  "SchoolType": {"@tc": "21474836", "#text": "OTHER DAYS"},
  "SchoolStatus": {"@tc": "1", "#text": "DOING OKAY"},
  "Extension": {"Extension_School": {"AdditionalDetails": "CHRISTMAS DAY"}}}]
}

我想为每个Teacher@id提取Teacher信息(TeacherTypeGender等),其中相关的SchoolType.\"#text\"包含所有School的任何School@id的“OTHER”。
我尝试了下面的查询,但它不起作用:

School[?SchoolType.\"#text\".contains(@, "OTHER")].Teacher[*].TeacherType.\"#text\"[]]
cgvd09ve

cgvd09ve1#

你必须以这种方式围绕你的条件数组扭曲contains函数:

[?contains(SchoolType."#text", 'OTHER')]

因此,获取完整Teacher对象的方法是:

School[?contains(SchoolType."#text", 'OTHER')].Teacher

或者,使用flatten操作符来摆脱数组的数组:

School[?contains(SchoolType."#text", 'OTHER')].Teacher | []

这将给予:

[
  {
    "@id": "XYZ_1",
    "TeacherType": {
      "@tc": "5",
      "#text": "GENDER"
    },
    "Gender": "FEMALE",
    "Extension": {
      "@VC": "23",
      "Extension_Teacher": {
        "DateDuration": {
          "@tc": "10023111",
          "#text": "0-6 MONTHS"
        }
      }
    }
  },
  {
    "@id": "XYZ_2",
    "TeacherType": {
      "@tc": "23",
      "#text": "EDUCATED"
    },
    "Extension": {
      "@VC": "23",
      "Extension_Teacher": {
        "DateDuration": {
          "@tc": "10023111",
          "#text": "CURRENT"
        }
      }
    }
  }
]

相关问题