I'm looking for breaking nested JSON file and try to flat it to fit into a SQL database.
Current JSON:
{
"content": {
"failedPerProductLineAndReason": {
"Product1": {
"Downsizing licenses is not allowed": 1
}
}
}
}
Expected outcome:
{
"ErrorType": "failedPerProductLineAndReason",
"product": "Product1",
"error": "Downsizing licenses is not allowed",
"quantity": 1
}
Flat nested JSON file
CodePudding user response:
Go inside the Downsizing licenses is not allowed
and get its value by @
and get another keys you want by $
[
{
"operation": "shift",
"spec": {
"*": { // content
"*": { // failedPerProductLineAndReason
"*": { // Product1
"*": { // Downsizing licenses is not allowed
"$2": "ErrorType",
"$1": "product",
"$": "error",
"@": "quantity"
}
}
}
}
}
}
]
CodePudding user response:
You can use $ wildcards incrementally nested within objects/attribute such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"$": "ErrorType",
"*": {
"$": "product",
"*": {
"$": "error",
"@": "quantity"
}
}
}
}
}
}
]
as principally needed to extract the keys