- Subscribe to RSS Feed
- Mark Question as New
- Mark Question as Read
- Float this Question for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
JSON object must always be treated as an array
- Labels:
-
Apache NiFi
Created ‎10-12-2022 04:14 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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"
}
Created on ‎10-17-2022 11:48 AM - edited ‎10-17-2022 11:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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"
}
}
}
]
Created ‎10-14-2022 04:23 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Created ‎10-17-2022 12:03 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Created on ‎10-17-2022 11:48 AM - edited ‎10-17-2022 11:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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"
}
}
}
]
Created ‎10-17-2022 11:23 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@ChuckE that's it. Thanks for your support in this case. Now, it works fine.

- « Previous
-
- 1
- 2
- Next »