skip to Main Content

I have an input json in the form of key, value pair.
I don’t want to include the key in the output if value for that key is missing. As in this input
teacherid is missing, so it shouldn’t be included in the ouput.

input :

{
  "alldetails": {
    "classid": 1,
    "schoolid": 3
  }
}

jolt spec :

[
  {
    "operation": "shift",
    "spec": {
      "alldetails": {
        "#Id_class": "data[0].source",
        "classid": "data[0].value",
        "#Id_teacher": "data[1].source",
        "teacherid": "data[1].value",
        "#Id_school": "data[2].source",
        "schoolid": "data[2].value"
      }
    }
  }
]

current output :

{
  "data": [
    {
      "source": "Id_class",
      "value": 1
    },
    {
      "source": "Id_teacher"
    },
    {
      "source": "Id_school",
      "value": 3
    }
  ]
}

desired output :

{
  "data": [
    {
      "source": "Id_class",
      "value": 1
    },
    {
      "source": "Id_school",
      "value": 3
    }
  ]
}

Any help would be appreciated. Thanks

2

Answers


  1. Chosen as BEST ANSWER

    This worked for me

    [
      {
        "operation": "shift",
        "spec": {
          "alldetails": {
            "classid": {
              "#Id_class": "data[0].source",
              "@(1,classid)": "data[0].value"
            },
            "teacherid": {
              "#Id_teacher": "data[1].source",
              "@(1,teacherid)": "data[1].value"
            },
            "schoolid": {
              "#Id_school": "data[2].source",
              "@(1,schoolid)": "data[2].value"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=recursivelySquashNulls"
        }
      }
    ]
    

  2. No need to hardcode but a shift transformation in the following case will handle the issue without prefixing the values with Id_, and to do this apply a modify-overwrite transformation such as

    [
      { // generating sub-objects
        "operation": "shift",
        "spec": {
          "alldetails": {
            "*id": {
              "*": {
                "@1": "data[#3].value",
                "$(1,1)": "data[#3].source" // replicates the values represented by asterisk 
                                            // located within "*id" after going two levels up
                                            // the tree counting by 0,1, eg. $(1, )
                                            // and pick the first asterisk, eg. $( ,1)
              }
            }
          }
        }
      },
      { // prefixing the values for the sources attributes
        "operation": "modify-overwrite-beta",
        "spec": {
          "data": {
            "*": {
              "s*": "=concat('Id_',@(1,&))"
            }
          }
        }
      }
    ]
    

    the input already is not got to do with teacher

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here

    Edit : Based on your need, you might construct the transformation starting from an input which’s preety similar to yours such as

    [
      {
        "operation": "shift",
        "spec": {
          "alldetails": {
            "#Id_class": "[0].source",
            "classid": "[0].value",
            "#Id_teacher": "[1].source",
            "teacherid": "[1].value",
            "#Id_school": "[2].source",
            "schoolid": "[2].value"
          }
        }
      },
      { // add "value" attributes with a specific value(ToBeDeleted) if those don't exist currently per each object
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "~value": "ToBeDeleted"
          }
        }
      },
      { // set names for the objects
        "operation": "shift",
        "spec": {
          "*": {
            "*": "@(1,value).&"
          }
        }
      },
      { // conditionally pick the objects
        "operation": "shift",
        "spec": {
          "ToBeDeleted": {
            "": "&"
          },
          "*": {
            "@": "data[]"
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search