是否有一种方法可以从顶部数组访问值,同时还可以使用jmespath从嵌套数组获取信息?

时间:2019-06-24 08:23:26

标签: jmespath

尝试将带有嵌套对象的json转换为多个对象时遇到问题。在下面查看我的json,您可以看到我得到的示例

{
  "name": "Isa Hemd ",
  "colors": [
    {
      "color": "Deep Forest",
      "sizes": [
        {
          "GTIN": "5712973396589",
          "size": "34"
        },
        {
          "GTIN": "5712973396596",
          "size": "36"
        },
        {
          "GTIN": "5712973396602",
          "size": "38"
        },
        {
          "GTIN": "5712973396619",
          "size": "40"
        },
        {
          "GTIN": "5712973396626",
          "size": "42"
        },
        {
          "GTIN": "5712973396633",
          "size": "44"
        },
        {
          "GTIN": "5712973396640",
          "size": "46"
        },
        {
          "GTIN": "5712973396657",
          "size": "48"
        }
      ],
      "code_color": "59141"
    }
  ],
  "materials": [
    {
      "Viscose": "65"
    },
    {
      "Modal": "35"
    }
  ],
  "description": "Dieses Hemd ist ein echtes Lieblingsstück im Kleiderschrank. Es hat ein klassisches Hemddesign mit Hemdkragen und langen Ärmeln. Die Knopfleiste des Hemds wird durch ein elegantes Streifen-Design abgerundet.  Das Hemd besteht darüber hinaus zu 100%  aus Viskose, die sich leicht und angenehm auf der Haut anfühlt. Ihre Kleidung von LauRie wird ohne Allergene und gesundheitsschädliche Chemikalien hergestellt.",
  "style_number": "30821"
}

我想为所有颜色->尺寸组合(例如

)制作单独的对象
 {
      "color": "Deep Forest",
      "size": "34",
      "GTIN": "5712973396589"
 },
 {
      "color": "Deep Forest",
      "size": "36",
      "GTIN": "5712973396596"
 }
 ...

具有更多数据的c。可能正在使用。

但是使用colors [*] .sizes [*]时,我看不到如何在进入尺寸之前先从颜色中获取值。认为也许使用数组索引是一种方法,因为我可以对其稍加修改,然后保存内容以供日后获取,但是显然也无法获得该值:)

对于x和y的每种组合,我如何将具有大量嵌套数组的json转换为多个对象?

目的是稍后将每个对象组合的一行转换为excel,在此之前,我可以使用jmespath进行转换,然后使用jsonschema验证内容。

1 个答案:

答案 0 :(得分:0)

我找到了一种硬编码的方法来获取结果,如果有时间,我将尝试动态地进行处理,但是现在这是硬编码的查询:

@.{sizes: sizes[*], colors: {color: color}} | merge({colors: colors}, sizes[*]) | [merge("0","colors"),merge("1","colors"),merge("2","colors"),merge("3","colors"),merge("4","colors"),merge("5","colors"),merge("6","colors"),merge("7","colors")]

它给出:

[
  {
    "GTIN": "5712973396589",
    "size": "34",
    "color": "Deep Forest"
  },
  {
    "GTIN": "5712973396596",
    "size": "36",
    "color": "Deep Forest"
  },
  {
    "GTIN": "5712973396602",
    "size": "38",
    "color": "Deep Forest"
  },
  {
    "GTIN": "5712973396619",
    "size": "40",
    "color": "Deep Forest"
  },
  {
    "GTIN": "5712973396626",
    "size": "42",
    "color": "Deep Forest"
  },
  {
    "GTIN": "5712973396633",
    "size": "44",
    "color": "Deep Forest"
  },
  {
    "GTIN": "5712973396640",
    "size": "46",
    "color": "Deep Forest"
  },
  {
    "GTIN": "5712973396657",
    "size": "48",
    "color": "Deep Forest"
  }
]
相关问题