skip to Main Content

I have input with more than 1000 keys in input and there are few objects in my input as well with key value pairs.

I need to transform all the key values to string inside each object as well as outer objects.
When I am using =toString in individual object it is flatting the objects to string as shown in output mentioned below.

Input:

{
  "value": 3,
  "primary": {
    "value": 3
  },
  "quality": {
    "value": 3
  }
}

Required Output :

 {
  "value" : "3",
  "primary" : {
    "value" : "3"
  },
  "quality" : {
    "value" : "3"
  }
}

Jolt tried

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "primary": {
        "value": "=toString"
      },
      "*": "=toString"
    }
  }
]

Output coming from my jolt:

{
  "value" : "3",
  "primary" : "{value=3}",
  "quality" : "{value=3}"
}

2

Answers


  1. You rather can use shift transformations consecutively in this case such as

    [
      { //Distinguish the Outermost part(key-value pairs) from 
        //the nested ones(objects) while replicating the exact input  
        "operation": "shift",
        "spec": {
          "*": {
            "$": "Outermost.@0[]",
            "@": "&1" //replicate the input
          }
        }
      },
      { //Keep only the nested ones, 
        //eg. get rid of the non-nested pairs
        //while switching back to the original pairs
        //for the elements within the "Outermost" object
        "operation": "shift",
        "spec": {
          "Outermost": {
            "*": {
              "*": {
                "$1": "@0"
              }
            }
          },
          "*": {
            "*": {
              "*": {
                "$": "&3.&2"
              }
            }
          }
        }
      }
    ]
    

    which will dynamically convert those integers to quoted values, no matter elements you have in the JSON provided the presented model kept for the whole structure.

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    enter image description here

    Login or Signup to reply.
  2. You could use String-based approach:

    jsonStr = jsonStr.replaceAll(": ([^{}",\s]+)", ": "$1"");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search