首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JOLT:规范JSON

JOLT:规范JSON
EN

Stack Overflow用户
提问于 2022-10-13 12:28:37
回答 1查看 33关注 0票数 2

我是新来的震动,只是不能理解有关列表的语法。我有以下JSON:

代码语言:javascript
复制
{
  "Country": [
    {
      "Name": "Angola",
      "AnimalNames.AnimalName": [
        "Aspidelaps lubricus",
        "Atheris squamigera",
        "Atractaspis bibronii"
      ]
    },
    {
      "Name": "Argentina",
      "AnimalNames.AnimalName": [
        "Apis mellifera scutellata",
        "Bothrops alternatus",
        "Bothrops ammodytoides"
      ]
    }    
  ]
}

期望产出:

代码语言:javascript
复制
[
   {
      "country_name":"Angola",
      "species":"Aspidelaps lubricus"
   },
   {
      "country_name":"Angola",
      "species":"Atheris squamigera"
   },
   {
      "country_name":"Angola",
      "species":"Atractaspis bibronii"
   },
   {
      "country_name":"Argentina",
      "species":"Apis mellifera scutellata"
   },
   {
      "country_name":"Argentina",
      "species":"Bothrops alternatus"
   },
   {
      "country_name":"Argentina",
      "species":"Bothrops ammodytoides"
   }
]

我所有的尝试都给了我一份所有国家的名单和所有物种的名单。

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-13 12:57:00

可以使用以下shift转换规范,如所解释的方式所示

代码语言:javascript
复制
[
  {
    "operation": "shift",
    "spec": {
      "Country": {
        "*": { // level of the indexes of the "Country" array        
          "AnimalNames*": { // represents the key-value pair(attribute,object or array) starting with animal
            "*": { // the indexes of the current array(if it was an object this level wouldn't exist)
              "@(2,Name)": "@(3,Name)[&1].country_name", // group by the countries and their respective sub-indexes within the arrays where "@(2,Name)" represents going two levels up the tree and grabbing the value of "Name" attribute
              "@": "@(3,Name)[&1].species" // go three levels up the tree by traversing : and double { in order to reach the level of the Country Name while indexing with "AnimalNames.AnimalName" arrays by [&1] to go one level up as keeping array style by square brackets and "species" is a fixed tag name 
            }
          }
        }
      }
    }
  },
  {
    // get rid of the array tags
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74055800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档