json 如何使用jolt规范遍历数组

eimct9ow  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(97)

下面是inputJSON:

{
  "c2": [
    "142.11.242.31:443",
    "192.119.110.73:443"
  ]
}

字符串

期望输出

{
  "iocs": [
    {
      "ioc": {
        "ioc_type": "sha256",
        "value": "test value"
      },
      "metadata": {
        "meta": {
          "tags": [
            "tag1",
            "tag2",
            "tag3"
          ]
        },
        "source": "test",
        "datetime_observed": "test"
      }
    },
    {
      "ioc": {
        "ioc_type": "ip",
        "value": "142.11.242.31:443"
      },
      "metadata": {
        "meta": {
          "tags": [
            "tag1",
            "tag2",
            "tag3"
          ]
        },
        "source": "test",
        "datetime_observed": "test"
      }
    },
    {
      "ioc": {
        "ioc_type": "ip",
        "value": "192.119.110.73:443"
      },
      "metadata": {
        "meta": {
          "tags": [
            "tag1",
            "tag2",
            "tag3"
          ]
        },
        "source": "test",
        "datetime_observed": "test"
      }
    }
  ]
}


转换的第一部分我能够解决它。我试图弄清楚如何循环通过c2列表和转换的方式,我可以创建具有自己属性的单独json对象。
请参见下面的当前规范

[
  {
    "operation": "default",
    "spec": {
      "ioc_data": {
        "iocs": [
          {
            "ioc": {
              "ioc_type": "sha256",
              "value": "test value"
            },
            "metadata": {
              "meta": {
                "tags": [
                  "tag1",
                  "tag2",
                  "tag3"
                ]
              },
              "source": "test",
              "datetime_observed": "date"
            }
          }
        ]
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "c2": "c2",
      "ioc_data": {
        "*": "&"
      }
    }
  }
]


谢谢您,期待您的指导。

ijxebb2r

ijxebb2r1#

主要需要**"@""value"匹配,其余部分是硬编码,其中一部分可以通过默认规范作为整个对象处理,就像您已经做的那样,其他部分通过使用带有移位转换的#**通配符来处理,例如

[
  {
    "operation": "default",
    "spec": {
      "iocs": {
        "ioc": {
          "ioc_type": "sha256",
          "value": "test value"
        },
        "metadata": {
          "meta": {
            "tags": [
                  "tag1",
                  "tag2",
                  "tag3"
                ]
          },
          "source": "test",
          "datetime_observed": "date"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "c2": {
        "*": { // indexes of the "c2" array 
          "#ip": "iocs[&1].ioc.ioc_type",
          "@": "iocs[&1].ioc.value",
          "#tag1|#tag2|#tag3": "iocs[&1].metadata.meta.tags",
          "#test": ["iocs[&1].metadata.source", "iocs[&1].metadata.datetime_observed"]
        }
      },
      "*": "&" // keeping the default object through use of this
    }
  }
]

字符串
网站https://jolt-demo.appspot.com/上的 * 演示 * 是:


的数据

相关问题