0

我有以下 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 
}]}

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

4

1 回答 1

0

看起来应该很简单,但是 Shift 不能匹配不同的东西,如果它是一个映射或一个标量。在这种情况下,输入中的“值”有时是标量,有时是嵌套映射。

如果您知道多值的“名称”,那么 Jolt 可以提供帮助。

规格

[
  {
    // 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"
            }
          }
        }
      }
    }
  }
]
于 2017-09-13T02:18:21.947 回答