skip to Main Content

I have a string field that represents JSON value

{ "value": "[{"field_1": "123","field_2": "id","field_3": "qwe"}]" }

Is there a way in jolt to convert it to JSON, something like this

{ "value": [{"field_1": "123","field_2": "id","field_3": "qwe"}] }

I know format of objects in array, there always will be only three fields with same names, but the number of objects can vary

Also i wounder is there a way to do the opposite thing, like convert array to string

2

Answers


  1. { "value": "[{"field_1": "123","field_2": "id"}]" }
    you can try this text , because your text missing a character " { "

    Login or Signup to reply.
  2. You can use successive split and join functions as in the following specification considering the fact

    there always will be only three fields with same names, but the number of objects can vary :

    [
      { // generate an array, namely "value", with contents of each array which are purified from " characters
        "operation": "modify-overwrite-beta",
        "spec": {
          "val_0": "=split(",@(1,value))",
          "val_1": "=join('',@(1,val_0))",
          "val_2": "=split('\[\{',@(1,val_1))",
          "val_3": "=join('',@(1,val_2))",
          "val_4": "=split('\}\]',@(1,val_3))",
          "val_5": "=join('',@(1,val_4))",
          "value": "=split('\},\{',@(1,val_5))"
        }
      },
      { // dissipate each components of the "value" array to form a new array of objects
        "operation": "shift",
        "spec": {
          "value": {
            "*": {
              "@": "[&1].val"
            }
          }
        }
      },
      { // make "value" array with key-value-key-value-key-value ordered components 
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "v": "=split(',',@(1,val))",
            "val": "=join(':',@(1,v))",
            "value": "=split(':',@(1,val))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@(0,value[1])": "[#2].@(1,value[0])", // represents the key-value pair for "field_1"
            "@(0,value[3])": "[#2].@(1,value[2])", // represents the key-value pair for "field_2"
            "@(0,value[5])": "[#2].@(1,value[4])" // represents the key-value pair for "field_3"
          }
        }
      },
      { // get rid of the leading whitespaces of the values
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": "=trim"
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search