如何删除以1结尾的数组元素?

时间:2018-10-29 13:12:20

标签: json bash jq

我需要删除所有以AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(new AssemblyName("MyAssembly"), AssemblyBuilderAccess.RunAndSave); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("ModuleName", "MyAssembly.dll"); TypeBuilder typeBuilder = moduleBuilder.DefineType("MyNamespace.TypeName", TypeAttributes.Public); typeBuilder.DefineDefaultConstructor(MethodAttributes.Public); // Build the method 'public int ReturnTheAnswer() => 42;'. MethodBuilder newMethod = typeBuilder.DefineMethod("ReturnTheAnswer", MethodAttributes.Public, typeof(int), new Type[0]); ILGenerator ilGen = newMethod.GetILGenerator(); ilGen.Emit(OpCodes.Ldc_I4_S, 42); ilGen.Emit(OpCodes.Ret); Type newType = typeBuilder.CreateType(); assemblyBuilder.Save("MyAssembly.dll"); // Save the assembly in the programs work directory ('bin\Debug'). dynamic o = Activator.CreateInstance(newType); // Create an instance of the dynamically created type. int r = (int) o.ReturnTheAnswer(); Debug.Assert(r == 42); // If this doesn't fail, the type has been built correctly, is in fact in the .dll and can be used perfectly fine. 结尾的name字段的数组元素。

输入:

1

所需的输出:

{
  "foo": "bar",
  "data": {
    "code": "abc123",
    "items": [
      {
        "name": "exp1"
      },
      {
        "name": "exp2"
      },
      {
        "name": "exp11"
      }
    ]
  }
}

我的尝试

{
  "foo": "bar",
  "data": {
    "code": "abc123",
    "items": [
      {
        "name": "exp2"
      }
    ]
  }
}

这将产生jq 'del(.data.items[] | select(.name | endswith("1")))' input

2 个答案:

答案 0 :(得分:4)

您可以使用以下<过滤器:

jq

此操作将jq '.data.items|=map(select(.name|endswith("1")|not))' file 替换为一个新数组,该数组包含对象的.data.items不以name结尾的对象。

答案 1 :(得分:2)

  1. 您的尝试将适用于jq的最新版本(即比1.5版更高的版本)。

  2. 还有另一个变体(也许是最简洁的健壮替代品):

    .data.items|=map(select(.name|test("[^1]$")))