如何遍历列表并替换其中的一些元素? 我有这样的输入

{
  "clients": [
    {
      "clientId": "166734",
      "info": {
        "cards": [
          "378282246310005",
          "371449635398431"
        ]
      }
    }
  ]
}

我所期望的卡片会是这样的100

但在我的规范中,子字符串不起作用

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "clients": {
        "*": {
          "info": {
            "cards": {
              "*": "=substring(@(1,&),0,@(1,4))"
            }
          }
        }
      }
    }
  }
]

推荐答案

您可以使用shift个转换来驯服卡片数组,以便为modify个转换规范做准备,例如

[
  {
    "operation": "shift",
    "spec": {
      "clients": {
        "*": {
          "*": "&2[#2].&",
          "info": {
            "cards": {
              "*": {
                "@": "&5[#5].&3.&2.&"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "clients": {
        "*": {
          "info": {
            "cards": {
              "*": "=substring(@(1,&),0,4)"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "clients": {
        "*": {
          "*": "&2[#2].&",
          "info": {
            "cards": {
              "*": "&4[#4].&2.&1[#1]"
            }
          }
        }
      }
    }
  }
]

如果101总是只能有两个组件,那么我们可以通过使用102个函数来缩短整个规范,否则可以通过单独编写以下代码来使用100

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "clients": {
        "*": {
          "info": {
            "c0": "=firstElement(@(1,cards))", //or "=(@(1,cards[0]))"
            "c1": "=lastElement(@(1,cards))",  //or "=(@(1,cards[1]))"
            "cards0": "=substring(@(1,c0),0,4)",
            "cards1": "=substring(@(1,c1),0,4)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "clients": {
        "*": {
          "*": "&2[#2].&",
          "info": {
            "*s*": "&3[#3].&1.&(0,1)s"
          }
        }
      }
    }
  }
]

Json相关问答推荐

如何在对象投影(*)上应用滤镜投影([?port==`eth1`])?

如何使用Aeson解码带有Unicode字符的JSON文件?

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

组合不同属性的Jolt Spec

如何从一个700MB的json文件中列出PowerShell中的所有密钥?

如何将属性拆分为嵌套的JSON内容?

带有 API 测试的 Typescript JSON 模式验证

Groovy JsonBuilder 在for循环中添加数组元素

jq :当路径可变时更新 json 内容

将请求中的数据推送到数组中

转义 Haskell 记录的字段

如何删除 django jsonfield 中的特定项目

将 ES6 类对象序列化为 JSON

Spring MVC 4:application/json内容类型设置不正确

如何通过 NSJSONSerialization 在 JSON 中包含空值?

jQuery fullcalendar 发送自定义参数并使用 JSON 刷新日历

使用 API 搜索维基百科

如何在 JAVA 中对 JSONArray 进行排序

有没有一种快速的方法可以在文本编辑器中将 JavaScript 对象转换为有效的 JSON?

通过url获取json数据并在python中使用(simplejson)