json 如果值不存在,则震动转换以删除关键字

jtoj6r0c  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(90)

我有一个以键,值对的形式输入的json。如果缺少该键的值,我不想在输出中包含该键。因为在这个输入中缺少teacherid,所以它不应该包含在输出中。

输入

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

字符串

震动规格

[
  {
    "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"
      }
    }
  }
]

电流输出

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

期望输出

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


任何帮助都将不胜感激。谢谢

yxyvkwin

yxyvkwin1#

不需要硬编码,但在下面的情况下,shift转换将在不使用**Id_作为值前缀的情况下处理该问题,并为此应用modify-overwrite**转换,例如

[
  { // 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,&))"
        }
      }
    }
  }
]

字符串
输入已经与**teacher**无关
网站http://jolt-demo.appspot.com/上的 * 演示 * 是:
x1c 0d1x的数据
编辑:根据您的需要,您可以从与您的输入非常相似的输入开始构建转换,例如

[
  {
    "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[]"
      }
    }
  }
]

相关问题