0

I got an issue when trying to convert my json with nested objects into several objects. Looking at my json below, you can see an example of what i got

{
  "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"
}

I want to make individuel objects for all color -> size combinations such as

 {
      "color": "Deep Forest",
      "size": "34",
      "GTIN": "5712973396589"
 },
 {
      "color": "Deep Forest",
      "size": "36",
      "GTIN": "5712973396596"
 }
 ...

With more data ofc. maybe being used.

However using colors[ * ].sizes[ * ] i can't see how i first can grab values from colors before heading into sizes. Thought that maybe using the array index was a way, as i could then hack it a bit and save content to be fetched later, but apparently its not possible to get that one either :)

How can i convert my json with a lot of nested arrays into multiple objects for each combination of x and y ?

The purpose is to later convert it into excel with a row for each object combination, where i could use jmespath for conversion and jsonschema to validate the content before doing so.

1 Answers1

1

I find a hard-coded way to get the result, I will try to do it dynamically if I have time to do it, but for now here is the hard-coded query:

colors[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")]

it give:

[
  {
    "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"
  }
]
bosskay972
  • 890
  • 1
  • 6
  • 27