3

I have a JSON object:

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

Desired 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.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Harsh
  • 75
  • 5

2 Answers2

0

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]}"
}
Matthew Warman
  • 3,234
  • 2
  • 23
  • 35
0

Indeed current case is pretty good, you're so close, just need two modifications to the current key ("*field2:*}*") should be applied such that

  • Remove the asterisk at the end. Since, it expects at least one character following }, and fails in the current case
  • Leave one whitespace before second asterisk to prune the resultant literal "z" rather than " z"

So, convert "*field2:*}*" to "*field2: *}"

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

or you can use the literal fiel after reshaping the key as follows and get the same result

[
  {
    "operation": "shift",
    "spec": {
      "Data": {
        "*],*d2: *}": {
          "$(0,3)": "&(1,2)d3"
        }
      }
    }
  }
]
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55