通过使用内部属性合并数组

时间:2019-02-25 16:11:09

标签: json jq

我有不同的语言文件,例如:

文件1

import pretrainedmodels

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=64
                                  ).normalize(imagenet_stats)

arch = pretrainedmodels.__dict__['alexnet'](num_classes=1000,pretrained='imagenet')
arch.last_linear.out_features = data.c

model = nn.Sequential(OrderedDict([
    ('features', arch._features),
    ('classifier', nn.Sequential(Flatten(), *children(arch)[1:]))
]))

learn = create_cnn(data, lambda *args : model, metrics=error_rate, custom_head=net.classifier)

# model says 37 classes in last linear
print(learn.model)
# summary incorrectly says 1000 classes in last linear
print(learn.summary())

文件2

{
    "Pack": [
        {
            "id": "item1",
            "lang": {
                "en": {
                }
            }
        },
        {
            "id": "item2",
            "lang": {
                "en": {
                }
            }
        }
    ]
}

,我需要通过 lang 字段合并相同的ID。最终文件应如下所示:

{
    "Pack": [
        {
            "id": "item1",
            "lang": {
                "sp": {
                }
            }
        }
    ]
}

我认为我应该使用更复杂的命令,但我的出发点是:

{
    "Pack": [
        {
            "id": "item1",
            "lang": {
                "en": {
                },
                "sp": {
                }
            }
        },
        {
            "id": "item2",
            "lang": {
                "en": {
                }
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

首先,您将要读取所有文件作为输入,然后将所有Pack项组合在一起,并按id将它们聚合成组,然后将这些组分类并排列成所需的结果。

$ jq -n '
{Pack: ([inputs.Pack[]] | group_by(.id) | map({id: .[0].id, lang: (map(.lang) | add)}))}
' file*.json

结果是:

{
  "Pack": [
    {
      "id": "item1",
      "lang": {
        "en": {},
        "sp": {}
      }
    },
    {
      "id": "item2",
      "lang": {
        "en": {}
      }
    }
  ]
}