bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 329 forks source link

Feature Request - toJSON #1028

Open thamblin opened 3 years ago

thamblin commented 3 years ago

Would it possible to have a function added for converting an element to a JSON encoded string? The toString function when applied to objects and arrays doesn't produce a true JSON string.

This is useful when the JSON you're processing is variable or the schema is not concrete and you want to create processes that will handle the input regardless of how the data changes


Example:

input

{
  "x": [ 3, 2, 1, "go"  ],
  "small": "small",
  "BIG": "BIG",
  "people": [
    {
      "firstName": "Bob",
      "lastName": "Smith",
      "address": {
        "state": null
      }
    },
    {
      "firstName": "Sterling",
      "lastName": "Archer"
    }
  ],
  "null_value": null,
  "boolean_value": false,
  "int_value":  13
}

spec

[{
  "operation": "modify-overwrite-beta",
  "spec": {
    "*": "=toJSON"
  }
}]

output

{
  "x" : "[3,2,1,\"go\"]",
  "small" : "\"small\"",
  "BIG" : "\"BIG\"",
  "people" : "[{\"firstName\":\"Bob\",\"lastName\":\"Smith\",\"address\":{\"state\":null}},{\"firstName\":\"Sterling\",\"lastName\":\"Archer\"}]",
  "null_value" : "null",
  "boolean_value" : "false",
  "int_value" : "13"
}
revoorunischal commented 3 years ago

already available https://github.com/bazaarvoice/jolt/pull/457/commits/2cf471d9fab06e724f46978e99c510e6e32138a2

opendpm commented 3 years ago

@revoorunischal, unfortunately, it was reverted then 😒 fded552