简体   繁体   中英

Extracting value from json using jolt transformation

I have a json object:

{

  "Data" : "{field1: [x,y],
               field2: z}",
}

Output json:

{

  "field3": "z"

}
[
  {
    "operation": "shift",
    "spec": {
      "Data": {
        "*field2:*}*": {
          "$(0,2)": "field3"
        }
      }
    }
  }
]

Here the value of "Data" is a complete string not a json, hence I have to break it into wildcards and now the 2nd '*' in the spec gives me the value "z". Is there any better approach to do the same such that say a new field comes before or after field2 then I don't have to modify this regex.

Firstly this doesn't feel like problem to be solved in JOLT, and instead the Data should be extracted and serialized to JSON.

That said i've managed to get something to work, explanation inline:

[
  //Remove {
  {
    "operation": "shift",
    "spec": {
      "Data": {
        "{*": {
          "$(0,1)": "Data"
        }
      }
    }
  },
  //Remove }
  {
    "operation": "shift",
    "spec": {
      "Data": {
        "*}": {
          "$(0,1)": "Data"
        }
      }
    }
  },
  // Split by ,
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Data": "=split(',', @(2,Data))"
    }
  },
  // Trim
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Data": {
        "*": "=trim"
      }
    }
  },
  // Select field2
  {
    "operation": "shift",
    "spec": {
      "Data": {
        "*": {
          "field2:*": {
            "$(0,1)": "field3"
          }
        }
      }
    }
  },
  // Trim again
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=trim"
    }
  }
]


Tested against:

{
  "Data": "{field1: [x,y], field2: z}"
}

and

{
  "Data": "{ field2: z, field1: [x,y]}"
}

and

{
  "Data": "{field1: [x,y], field2: z, field3: [x,y]}"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM