通过JOLT转换,借助以下方法将整个json文档弄平 [英] JOLT transformation to flatten the entire json document with help of

查看:306
本文介绍了通过JOLT转换,借助以下方法将整个json文档弄平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在json以下:

     {
    "deveui": "1000",
    "vars": [
        { "name": "CfgNo",
           "value":"255" },
        {
            "name":"Status","value":{
                "lampS": "unknown(0xff)",
                "lampL": "255"
            }
        },
        { "name" : "Volt", "value": "255" },
        {
            "name" : "gps", "value": {
                "lat": "12.93",
                "lon": "77.69"
             }
        },
        {"name" : "status", "value":"up"},
        {"name" : "last_status_change","value": 1503 }
    ]
} 

预期输出为:

{[{  "deveui": "1000",
  "CfgNo": "255",
  "lampS": "unknown(0xff)",
  "lampL": "255",
  "Volt": "255",
  "lat": "12.93",
  "lon": "77.69",
  "status": "up",
  "last_status_change": 1503 
}]}

是否可以转换为预期的输出格式.如果是这样,请帮助我设计规格.

is it possible to convert into expected output format. if so please help me to design spec.

推荐答案

它看起来应该很简单,但是Shift不能匹配其他内容(如果它是地图或标量).在这种情况下,输入中的值"有时是标量,有时是嵌套的图.

It looks like it should be simple, but Shift can't match something differently if it is a map or a scalar. In this case, "value" in your input is sometimes a scalar, and sometime a nested map.

如果您知道将包含多个值的名称",那么Jolt可以提供帮助.

If you know the "names" that will be multi values, then Jolt can help.

规格

[
  {
    // Shift can't vary its behavior between 
    //  the "value" key in the input being a 
    //  scalar or a map.
    // Thus the only way to do the desired flattening
    //  is if we know the names that will 
    //  be multi-valued.
    "operation": "shift",
    "spec": {
      // pass deveui thru
      "deveui": "deveui",
      "vars": {
        "*": { // array index of vars[]
          "name": {
            // match value of name Status or gps
            "Status|gps": {
              // We know these entries have "value" that is map.
              // For these, go back up the tree, 3 levels (0,1,2)
              // and come back down to the "value" entry.
              "@(2,value)": {
                // for each key in the value, pass it thru
                // to the output.
                "*": "&"
              }
            },
            "*": {
              // for all other values of "name" that are not
              //  Status or gps, just go back up the tree
              //  and grab the scalear "value" and write it
              //  to the output at the matched value of name.
              "@(2,value)": "&1"
            }
          }
        }
      }
    }
  }
]

这篇关于通过JOLT转换,借助以下方法将整个json文档弄平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆