将键和值附加到多个JSON文件中的一个JSON对象

时间:2016-07-21 01:28:16

标签: javascript python json

我目前正在处理三个json文件。

master.json

 {
  "master":[
    {
      "sport": "soccer",
      "id": 1
    },
    {
      "sport": "football",
      "id": 2
    }
  ]
}

data1.json

{
  "soccer": {
    "players": 11
  },
  "football": {
    "players": 12
  }
}

data2.json

{
  "soccer": {
    "stadiums": {
      "away": "StadiumA",
      "home": "StadiumB"
    }
  },
  "football": {
    "stadiums": {
      "away": "StadiumA",
      "home": "StadiumB"
    }
  }
}

我想做的是将每个json文件中每个运动的属性合并到master.json

这就是我要找的:

{
  "master":[
    {
      "sport": "soccer",
      "id": 1,
      "players": 11,
      "stadiums": {
        "away": "StadiumA",
        "home": "StadiumB"
      }

    },
    {
      "sport": "football",
      "id": 2,
      "players": 12,
      "stadiums": {
        "away": "StadiumA",
        "home": "StadiumB"
      }
    }
  ]
}

理想情况下,我希望能够包含一个条件,其中data1data2中的运动值必须与master.json中的运动值相匹配才能进行追加。因此,如果“棒球”在data1data2但不在master,则不包括在内。

我尝试在JavaScript中使用underscore,在Python中使用json,但没有运气。我似乎无法找到一种方法来遍历所有三个json文件,让他们互相交谈,特别是看看这项运动是否匹配。

任何帮助都会受到赞赏。我也会尽力澄清和混淆。

2 个答案:

答案 0 :(得分:1)

这样做。请记住,这是适合你的情况。

var master = {
  "master":[
    {
      "sport": "soccer",
      "id": 1
    },
    {
      "sport": "football",
      "id": 2
    }
  ]
}

var data1 = {
  "soccer": {
    "players": 11
  },
  "football": {
    "players": 12
  }
}

var data2 = {
  "soccer": {
    "stadiums": {
      "away": "StadiumA",
      "home": "StadiumB"
    }
  },
  "football": {
    "stadiums": {
      "away": "StadiumA",
      "home": "StadiumB"
    }
  }
}

var masterSports = master.master.map(function(object){
    return object.sport;
});

var data1Keep = {};
var data2Keep = {};

Object.keys(data1).map(function(key){
    if(masterSports.indexOf(key) > -1 && !data1Keep.hasOwnProperty(key)){
        data1Keep[key] = data1[key];
    }
});
Object.keys(data2).map(function(key){
    if(masterSports.indexOf(key) > -1 && !data2Keep.hasOwnProperty(key)){
        data2Keep[key] = data2[key];
    }
});

master.master.map(function(object){
    for(key in data1Keep){
        if(key == object.sport){
            for(i in data1Keep[key]){
                object[i] = data1Keep[key][i];
            }
        }
    }

    for(key2 in data2Keep){
        if(key2 == object.sport){
            for(j in data2Keep[key2]){
                object[j] = data2Keep[key2][j];
            }
        }
    }
    return object;
});

答案 1 :(得分:1)

为了证明Python的相对技巧,我将提出一个解决方案:

import json
from collections import OrderedDict

with open('master.json') as m, open('data1.json') as d1, open('data2.json') as d2:
    master = json.load(m, object_pairs_hook=OrderedDict)
    data1 = json.load(d1, object_pairs_hook=OrderedDict)
    data2 = json.load(d2, object_pairs_hook=OrderedDict)

for i, obj in enumerate(master['master']):
    d1, d2 = data1.get(obj['sport']), data2.get(obj['sport'])
    if d1:
        master['master'][i].update(d1)
    if d2:
        master['master'][i].update(d2)

print(json.dumps(master,indent=2))

输出:

{
  "master": [
    {
      "sport": "soccer",
      "id": 1,
      "players": 11,
      "stadiums": {
        "away": "StadiumA",
        "home": "StadiumB"
      }
    },
    {
      "sport": "football",
      "id": 2,
      "players": 12,
      "stadiums": {
        "away": "StadiumA",
        "home": "StadiumB"
      }
    }
  ]
}