Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

JSON object must always be treated as an array

avatar
Contributor

Hello everyone.

i need help. I have a JSON file which contains in an object either an array of other objects or a single object. This is the relevant object "Product": [ or "Product": { .

How can I configure with the JOLT Transform processor to always expect an array in the object? The product object must always be treated as an array, even if it contains only a single object.

 

{
  "costc": 9638,
  "sum_amount": 543,
  "sum_invitation": 0,
  "sum_selfconsumption": 0,
  "sum_loss": 0,
  "sum_gross": "2764,000",
  "sum_net": "2392,480",
  "sum_margin": "2003,780",
  "sum_costs_total": "388,700",
  "sum_costs": "388,700",
  "sum_costs_invitation": 0,
  "sum_costs_selfconsumption": 0,
  "sum_costs_loss": 0,
  "sum_costs_total_percent": "16,25",
  "sum_costs_percent": "16,25",
  "Classes": {
    "ClassSummary": [
      {
        "class": 1102,
        "sum_amount": 117,
        "sum_invitation": 0,
        "sum_selfconsumption": 0,
        "sum_loss": 0,
        "sum_gross": "740,500",
        "sum_net": "692,060",
        "sum_margin": "552,385",
        "sum_costs_total": "139,675",
        "sum_costs": "139,675",
        "sum_costs_invitation": 0,
        "sum_costs_selfconsumption": 0,
        "sum_costs_loss": 0,
        "sum_costs_total_percent": "20,18",
        "sum_costs_percent": "20,18",
        "ProductDetails": {
          "Product": [
            {
              "id": 7992160,
              "artnr": 32212,
              "sum_amount": 16,
              "sum_invitation": 0,
              "sum_selfconsumption": 0,
              "sum_loss": 0,
              "sum_gross": "80,000",
              "sum_net": "74,770",
              "sum_margin": "53,243",
              "sum_costs_total": "21,527",
              "sum_costs": "21,527",
              "sum_costs_invitation": 0,
              "sum_costs_selfconsumption": 0,
              "sum_costs_loss": 0,
              "sum_costs_total_percent": "28,79",
              "sum_costs_percent": "28,79",
              "value": null
            },
            {
              "id": 7794909,
              "artnr": 32205,
              "sum_amount": 7,
              "sum_invitation": 0,
              "sum_selfconsumption": 0,
              "sum_loss": 0,
              "sum_gross": "56,000",
              "sum_net": "52,340",
              "sum_margin": "39,473",
              "sum_costs_total": "12,867",
              "sum_costs": "12,867",
              "sum_costs_invitation": 0,
              "sum_costs_selfconsumption": 0,
              "sum_costs_loss": 0,
              "sum_costs_total_percent": "24,58",
              "sum_costs_percent": "24,58",
              "value": null
            },
            {
              "class": 19174,
              "sum_amount": 17,
              "sum_invitation": 0,
              "sum_selfconsumption": 0,
              "sum_loss": 0,
              "sum_gross": "85,000",
              "sum_net": "71,430",
              "sum_margin": "71,430",
              "sum_costs_total": 0,
              "sum_costs": 0,
              "sum_costs_invitation": 0,
              "sum_costs_selfconsumption": 0,
              "sum_costs_loss": 0,
              "sum_costs_total_percent": 0,
              "sum_costs_percent": 0,
              "ProductDetails": {
                "Product": {
                  "id": 7925180,
                  "artnr": 6201,
                  "sum_amount": 17,
                  "sum_invitation": 0,
                  "sum_selfconsumption": 0,
                  "sum_loss": 0,
                  "sum_gross": "85,000",
                  "sum_net": "71,430",
                  "sum_margin": "71,430",
                  "sum_costs_total": 0,
                  "sum_costs": 0,
                  "sum_costs_invitation": 0,
                  "sum_costs_selfconsumption": 0,
                  "sum_costs_loss": 0,
                  "sum_costs_total_percent": 0,
                  "sum_costs_percent": 0,
                  "value": null
                }
              }
            }
          ]
        }
      }
    ]
  },
  "date": "2022-10-06"
}

 

 

1 ACCEPTED SOLUTION

avatar
Expert Contributor

@MarioFRS Ah, you are correct.  I overlooked that additional nested Product.  That complicates things because the upper Product can be an array or not any array, which itself contains another ProductDetails.Product which also may or may not be an array.  Ugh!

To solve this I had to resort to chaining multiple transforms to account for all the possible formats that can exist. I may not have all the possible permutations you'll have, but you can follow the basic structure of these transforms and create new ones with additional levels if necessary.

It is important to note that the inner-most portions of the nested data need transformed first, so the order of the transforms starts with the inner-most data and ends with the outer-most data.  Keep that in mind if you need to add more transforms to account for additional permutations of the data.

 

Give this a try and let me know how it works for you.

 

 

[
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": {
                    "Product": "MANY"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "Product": "MANY"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "Product": "MANY"
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": {
            "Product": "MANY"
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "ClassSummary": "MANY"
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "ClassSummary": "MANY"
      }
    }
  }
]

 

 

View solution in original post

13 REPLIES 13

avatar
Expert Contributor

Hey @MarioFRS,  Just following up to make sure things are good to go.  If you are all set then please accept the solution to ensure it helps others in the future.

 

avatar
Contributor

Hey @ChuckE Sorry for the delay. It doesn't work for me, I'm using the latest Jolt specification you provided and the entire json file, with individual objects and the array of objects from product. The single product object in a class does not become an array

MarioFRS_0-1665990161157.jpeg

 

avatar
Expert Contributor

@MarioFRS Ah, you are correct.  I overlooked that additional nested Product.  That complicates things because the upper Product can be an array or not any array, which itself contains another ProductDetails.Product which also may or may not be an array.  Ugh!

To solve this I had to resort to chaining multiple transforms to account for all the possible formats that can exist. I may not have all the possible permutations you'll have, but you can follow the basic structure of these transforms and create new ones with additional levels if necessary.

It is important to note that the inner-most portions of the nested data need transformed first, so the order of the transforms starts with the inner-most data and ends with the outer-most data.  Keep that in mind if you need to add more transforms to account for additional permutations of the data.

 

Give this a try and let me know how it works for you.

 

 

[
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": {
                    "Product": "MANY"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "Product": "MANY"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "Product": "MANY"
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": {
            "Product": "MANY"
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "ClassSummary": "MANY"
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "ClassSummary": "MANY"
      }
    }
  }
]

 

 

avatar
Contributor

@ChuckE that's it. Thanks for your support in this case. Now, it works fine.