0

如何跳过所有路径访问错误?

input= {
   "firstname": "satish",
   "lastname": "reddy"     #may or may not be present
   "details" : {
               "phoneno": "987654321",
                "address": "xxxxx",   # may or may not be present
                "pincode": "xxxxx"   # may or may not be present
   },
   "familydetails": [
          {
               "name": "PersonA",
               "address": "Adress-A",
               "phoneno": "999999999"
           },
          {
               "name": "PersonB",
               "phoneno": "999999999"
           },
          {
               "name": "PersonC"
           }
    ]}

需要输出

output_needed = {
            "Captain": {
                     "FirstName": "satish",
                     "LastName": "reddy",   # include only if it is present in input
                     "PersonalDetails": {
                                         "MobileNo": "987654321",
                                         "Address": "xxxxx",   # include only if it is present in input
                                         "Pincode": "xxxxx"   # include only if it is present in input
                      } ,
                     "CaptainFamilyDetails": [
                                    {
                                         "Name": "PersonA", 
                                         "Address": "Adress-A",   # include only if it is present in input
                                         "MobileNo": "999999999" # include only if it is present in input
                                     },
                                    {
                                         "Name": "PersonB",
                                         "MobileNo": "999999999"
                                     },
                                    {
                                         "Name": "PersonC"
                                     }
                    ]
                }
}

使用的规格:

spec = {
  "Captain": {
       "FirstName": "firstname",
       "LastName": "lastname",
      "PersonalDetails": {
                  "MobileNo": "details.phoneno",
                   "Address":  "details.address",
                   "Pincode": "details.pincode"
              },
       "CaptainFamilyDetails" : ("familydetails", [
                  {
                     "Name": "name",
                     "MobileNo": "phoneno",
                     "Address": "address"
                  }
             ])
}

}

规范仅在提供所有必需参数时才有效,但如果缺少某些参数,则获取PathAccessError

我用可选字段尝试了Coalesce,但它对我来说不可行,我还有 25-30 个参数要映射,我们完全不知道哪些参数是可选的。

那么如何递归地跳过所有 PathAccessError ,以便即使发生 pathacesserror 也能继续执行?

4

0 回答 0