skip to Main Content

I have the following inputted Json:

{
    "extraFields": {
        "language": "french", 
        "status": {
            "comment": "hey"
        }
    }
}

Using Jolt, I want to achieve the following output:

{
    "extraFields": "{"language": "french", "status": {"comment": "hey"}}"
}

Note the backslashes.

I managed to do that with the following Jolt formula:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "extraFields": "=concat('{', '"language": ', '"', @(0,language), '" ,', '"status": ', '{', '"comment": ', '"', @(1,extraFields.status.comment), '"', '}', '}')"
    }
  }
]

Problem is, fields of extraFields are optional meaning, some of them might not be inputted.
For example, status can be omitted or language or both.
It results, empty values of non-inputted keys.

Is it possible to concatenate only inputted keys?

2

Answers


  1. Chosen as BEST ANSWER

    For the general case where extraFields resides as a key within a larger Json structure for instance, there's another key name at the same level i.e.

    {
      "name": "Michael Jackson",
      "extraFields": {
        "language": "french",
        "status": {
          "comment": "hey"
        }
      }
    }
    

    Aiming to keep all other fields intact and solely tweak extraFields.

    The corresponding Jolt would be:

    [
      {
        "operation": "shift",
        "spec": {
          "name": "name",
          "number": "number",
          "*": {
            "language": {
              "*": "&2."&1"."&""
            },
            "status": {
              "*": {
                "*": "&3."&2"."&1"."&""
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "name": "name",
          "number": "number",
          "*": {
            "*language*": {
              "*": {
                "$": "&3.&2"
              }
            },
            "*status*": {
              "*": {
                "*": {
                  "$": "&4.&3.&2"
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "extraFields": {
            "*": "=toString"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "temp1": "=toString(@(1,extraFields))",
          "temp2": "=split('=',@(1,temp1))",
          "extraFields": "=join(':',@(1,temp2))"
        }
      },
      {
        "operation": "remove",
        "spec": {
          "temp*": ""
        }
      }
    ]
    

    Credit to Barbaros Özhan


  2. You can pad the related characters by using a shift, then stringify the attribute by a modify transformation through use of toString function such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "language": {
              "*": "&2."&1"."&""
            },
            "status": {
              "*": {
                "*": "&3."&2"."&1"."&""
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*language*": {
              "*": {
                "$": "&3.&2"
              }
            },
            "*status*": {
              "*": {
                "*": {
                  "$": "&4.&3.&2"
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=toString"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "temp1": "=toString(@(1,extraFields))",
          "temp2": "=split('=',@(1,temp1))",
          "extraFields": "=join(':',@(1,temp2))"
        }
      },
      {
        "operation": "remove",
        "spec": {
          "temp*": ""
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search