首页 > 解决方案 > JMESPath Query exression

问题描述

I have a lists of Countries, States and Cities in below format.

[
  {
    "id": 1,
    "name": "Afghanistan",
    "states": [
      {
        "id": 3901,
        "name": "Badakhshan",
        "state_code": "BDS",
        "latitude": "36.73477250",
        "longitude": "70.81199530",
        "cities": [
          {
            "id": 52,
            "name": "Ashkāsham",
            "latitude": "36.68333000",
            "longitude": "71.53333000"
          },
          ....
        ]
      },
      ....
    ]
  },
  ....
]

I have formatted using JMESPath as:

[*].{id: id, name: name, states: states[*].[name, cities]}

The output is:

[
  {
    "id": 1,
    "name": "Afghanistan",
    "states": [
      [
        "Badakhshan",
        [
          "Ashkāsham",
          ....
        ]
      ],
      .....
    ]
   },
  ...
]

What will be the correct query to get the output below?

[
  {
    "id": 1,
    "name": "Afghanistan",
    "states": [
      [
        "Badakhshan": [
          "Ashkāsham",
           ....
        ]
      ],
      ...
    ]
  },
  ....
]

If there's any other solution or better format, it would be very much appreciated.

标签: jsonjmespath

解决方案


推荐阅读