8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

JOLT 规范对嵌套 json 执行按 ID 分组和条件检查

SuperUser 2月前

40 0

请帮我为以下输入 JSON 提供 JOLT 规范。尝试了多种方法,但仍然不起作用。风险属性有一个条件检查;如果风险为 \'0\',那么...

请帮我为以下输入 JSON 提供 JOLT 规范。尝试了多种方法,但仍然不起作用。风险属性有一个条件检查;如果风险为 \'0\',则风险等级低。如果是 \'1\',则风险等级高;如果为空,则应该为空。只有我得到了一个解决方案,但对于空值,输出 JSON 属性未显示。

输入 JSON:

[
  {
    "id": "12",
    "name": "Manu",
    "age": 26,
    "location": "New York",
    "risk": "0"
  },
  {
    "id": "12",
    "name": "Manju",
    "age": 29,
    "location": "New York",
    "risk": null
  },
  {
    "id": "123",
    "name": "sanju",
    "age": 24,
    "location": "New York city",
    "risk": "1"
  },
  {
    "id": "1234",
    "name": "Ramesh",
    "age": 23,
    "location": "India",
    "risk": null
  }
]

预期 JSON:

[
  {
    "id": "12",
    "location": "New York",
    "user": [
      {
        "name": "Manu",
        "age": 26,
        "riskLevel": "Low"
      },
      {
        "name": "Manju",
        "age": 29,
        "riskLevel": null
      }
    ]
  },
  {
    "id": "123",
    "location": "New York city",
    "user": [
      {
        "name": "sanju",
        "age": 24,
        "riskLevel": "High"
      }
    ]
  },
  {
    "id": "123",
    "location": "India",
    "user": [
      {
        "name": "Ramesh",
        "age": 23,
        "riskLevel": null
      }
    ]
  }
]
帖子版权声明 1、本帖标题:JOLT 规范对嵌套 json 执行按 ID 分组和条件检查
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由SuperUser在本站《json》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 您可以使用以下转换

    [ //set an arbitrary value(say "NUll") to a null valued "risk"
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "~risk": "NUll" //occures whnever risk is null due to the ~ operator 
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "@1,id.&[]", //the attributes other than the ones below
            "name|age": "@1,id.&1.&",
            "risk": {
              "1": { "#High": "@3,id.&3.riskLevel" }, //hardcode by # operator
              "0": { "#Low": "@3,id.&3.riskLevel" },
              "NUll": { "@": "@3,id.&3.riskLevel" }
            }
          }
        }
      },
      {//nest some attributes with array "user" while pick the first occurence fron the newly formed "id" and "location" arrays
        "operation": "shift",
        "spec": {
          "*": {
            "id|location": {
              "0": "&2.&1"
            },
            "*": "&1.user[]"
          }
        }
      },
      { //get rid of the wrapper keys of "id" values
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]
    
  • 感谢 Barbaros 提供的出色解决方案。我一直在向您学习,并因为您的解决方案而使 jolt 变得更好。我从来都不知道 \'~\' 在 jolt 中有特殊含义。请问您是如何发现它的?您使用的 jolt 指南是否有完整且最新的网址?

    此外,如果我可以为规范提供不同的方法:

    [
      {
        //group user info under id & location
        "operation": "shift",
        "spec": {
          "*": {
            "age": "@(1,id).@(1,location).[#2].age",
            "name": "@(1,id).@(1,location).[#2].name",
            "risk": {
              "0": {
                "#low": "@(3,id).@(3,location).[#4].risklevel"
              },
              "1": {
                "#high": "@(3,id).@(3,location).[#4].risklevel"
              }
            }
          }
        }
      }
      ,
      {
        // create final structure by
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "$1": "[#3].id",
              "$": "[#3].location",
              // dont use array for users yet to avoid getting nulls from above  
              "*": "[#3].user"
            }
          }
        }
      }
      ,
      {
        //ensure user is always an array
        "operation": "cardinality",
        "spec": {
          "*": {
            "user": "MANY"
          }
        }
      }
      ,
    
      {
        // set risklevel to null by default if doesnt exist
        "operation": "default",
        "spec": {
          "*": {
            "user[]": {
              "*": {
                "risklevel": null
              }
            }
          }
        }
      }
      /**/
    ]
    

    此外,由于我有使用 jslt 的经验,因此我想提供一种使用 jstl 解决这个问题的更简便的方法:

    import "http://jslt.schibsted.com/2018/experimental" as exp
    
    
        let groupBy= exp:group-by(.,
                                    {"id":.id,"location":.location},
                                    {"name":.name,"age":.age, "risklevel": if(.risk == "0") "low" else if (.risk=="1") "high" else null }
                                  )
        let result = [for ($groupBy) {"id":.key.id,"location":.key.location,"user":.values}]
        
        $result 
    
  • 嗨 Samer,谢谢你,很高兴听到这样的事情。您可以通过标记为 [jolt] 的问题 -> 了解更多... -> 最新发布:0.1.6 -> 发布 -> 标题下方 v0.0.22 新变换 \'modify\' 处于测试状态,晚安!

返回
作者最近主题: