skip to Main Content

I’m looking for breaking nested JSON file and trying to flatten them 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
}

2

Answers


  1. 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"
                }
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. 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

    the demo on the site http://jolt-demo.appspot.com/ is

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search