按键合并JSON数据 [英] Merge JSON Data by Key

查看:111
本文介绍了按键合并JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并天气数据中的几个JSON文件,并试图找出执行此操作的最佳方法.我发现其他几篇文章只是连接了JSON对象,或者在有相同键的情况下,第二篇文章覆盖了第一篇,等等,但是没有任何内容可以通过键将它们合并.

I am trying to merge several JSON files I have on weather data and am trying to figure out the best way to do it. I've found several other posts that just concatenate the JSON objects or that have the second one overwrite the first in case of the same key, etc but nothing that merges them by key.

下面是我正在使用的示例以及我想要的输出的示例.关于这个的任何建议都很棒!

Below is a sample of what I'm working with and the output I'd like. Any suggestions on this would be great!

示例JSON0.json

Sample JSON0.json

    {
    "1948": [
        {
            "Seattle": {
                "city": "Seattle",
                "data": {
                    "avg_dew_point": "34",
                    "avg_gust_wind": "",
                    "avg_max_temp": "45",
                    "avg_min_temp": "35",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.22",
                    "avg_temp": "40",
                    "avg_wind": "8"
                },
                "month": "1",
                "state": "WA"
            }
        },
        {
            "Chicago": {
                "city": "Chicago",
                "data": {
                    "avg_dew_point": "10",
                    "avg_gust_wind": "",
                    "avg_max_temp": "25",
                    "avg_min_temp": "11",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.17",
                    "avg_temp": "18",
                    "avg_wind": "12"
                },
                "month": "1",
                "state": "IL"
            }
        }
    ],
    "1949": [
        {
            "Houston": {
                "city": "Houston",
                "data": {
                    "avg_dew_point": "45",
                    "avg_gust_wind": "",
                    "avg_max_temp": "61",
                    "avg_min_temp": "44",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.15",
                    "avg_temp": "53",
                    "avg_wind": "12"
                },
                "month": "1",
                "state": "TX"
            }
        },
        {
            "Seattle": {
                "city": "Seattle",
                "data": {
                    "avg_dew_point": "25",
                    "avg_gust_wind": "",
                    "avg_max_temp": "38",
                    "avg_min_temp": "25",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.32",
                    "avg_temp": "31",
                    "avg_wind": "7"
                },
                "month": "1",
                "state": "WA"
            }
        }
    ]
}

示例JSON1.json

Sample JSON1.json

{
    "1948": [
        {
            "Jacksonville": {
                "city": "Jacksonville",
                "data": {
                    "avg_dew_point": "45",
                    "avg_gust_wind": "",
                    "avg_max_temp": "61",
                    "avg_min_temp": "44",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.13",
                    "avg_temp": "53",
                    "avg_wind": "8"
                },
                "month": "1",
                "state": "FL"
            }
        },
        {
            "Indianapolis": {
                "city": "Indianapolis",
                "data": {
                    "avg_dew_point": "13",
                    "avg_gust_wind": "",
                    "avg_max_temp": "34",
                    "avg_min_temp": "13",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.17",
                    "avg_temp": "24",
                    "avg_wind": "12"
                },
                "month": "1",
                "state": "IN"
            }
        }
    ],
    "1949": [
        {
            "San Jose": {
                "city": "San Jose",
                "data": {
                    "avg_dew_point": "",
                    "avg_gust_wind": "",
                    "avg_max_temp": "52",
                    "avg_min_temp": "33",
                    "avg_precipitation": "0.02",
                    "avg_sea_level_pressure": "",
                    "avg_temp": "43",
                    "avg_wind": ""
                },
                "month": "1",
                "state": "CA"
            }
        },
        {
            "Jacksonville": {
                "city": "Jacksonville",
                "data": {
                    "avg_dew_point": "55",
                    "avg_gust_wind": "",
                    "avg_max_temp": "73",
                    "avg_min_temp": "54",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.21",
                    "avg_temp": "63",
                    "avg_wind": "5"
                },
                "month": "1",
                "state": "FL"
            }
        }
    ]
}

样本组合的JSON.json

Sample combinedJSON.json

{
    "1948": [
        {
            "Seattle": {
                "city": "Seattle",
                "data": {
                    "avg_dew_point": "34",
                    "avg_gust_wind": "",
                    "avg_max_temp": "45",
                    "avg_min_temp": "35",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.22",
                    "avg_temp": "40",
                    "avg_wind": "8"
                },
                "month": "1",
                "state": "WA"
            }
        },
        {
            "Chicago": {
                "city": "Chicago",
                "data": {
                    "avg_dew_point": "10",
                    "avg_gust_wind": "",
                    "avg_max_temp": "25",
                    "avg_min_temp": "11",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.17",
                    "avg_temp": "18",
                    "avg_wind": "12"
                },
                "month": "1",
                "state": "IL"
            }
        },
        {
            "Jacksonville": {
                "city": "Jacksonville",
                "data": {
                    "avg_dew_point": "45",
                    "avg_gust_wind": "",
                    "avg_max_temp": "61",
                    "avg_min_temp": "44",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.13",
                    "avg_temp": "53",
                    "avg_wind": "8"
                },
                "month": "1",
                "state": "FL"
            }
        },
        {
            "Indianapolis": {
                "city": "Indianapolis",
                "data": {
                    "avg_dew_point": "13",
                    "avg_gust_wind": "",
                    "avg_max_temp": "34",
                    "avg_min_temp": "13",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.17",
                    "avg_temp": "24",
                    "avg_wind": "12"
                },
                "month": "1",
                "state": "IN"
            }
        }
    ],
    "1949": [
        {
            "Houston": {
                "city": "Houston",
                "data": {
                    "avg_dew_point": "45",
                    "avg_gust_wind": "",
                    "avg_max_temp": "61",
                    "avg_min_temp": "44",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.15",
                    "avg_temp": "53",
                    "avg_wind": "12"
                },
                "month": "1",
                "state": "TX"
            }
        },
        {
            "Seattle": {
                "city": "Seattle",
                "data": {
                    "avg_dew_point": "25",
                    "avg_gust_wind": "",
                    "avg_max_temp": "38",
                    "avg_min_temp": "25",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.32",
                    "avg_temp": "31",
                    "avg_wind": "7"
                },
                "month": "1",
                "state": "WA"
            }
        },
        {
            "San Jose": {
                "city": "San Jose",
                "data": {
                    "avg_dew_point": "",
                    "avg_gust_wind": "",
                    "avg_max_temp": "52",
                    "avg_min_temp": "33",
                    "avg_precipitation": "0.02",
                    "avg_sea_level_pressure": "",
                    "avg_temp": "43",
                    "avg_wind": ""
                },
                "month": "1",
                "state": "CA"
            }
        },
        {
            "Jacksonville": {
                "city": "Jacksonville",
                "data": {
                    "avg_dew_point": "55",
                    "avg_gust_wind": "",
                    "avg_max_temp": "73",
                    "avg_min_temp": "54",
                    "avg_precipitation": "0.00",
                    "avg_sea_level_pressure": "30.21",
                    "avg_temp": "63",
                    "avg_wind": "5"
                },
                "month": "1",
                "state": "FL"
            }
        }
    ]
}

推荐答案

尝试一下:

function mergeJson(target) {
    for (var argi = 1; argi < arguments.length; argi++) {
        var source = arguments[argi];
        for (var key in source) {
            if (!(key in target)) {
                target[key] = [];
            }
            for (var i = 0; i < source[key].length; i++) {
                target[key].push(source[key][i]);
            }
        }
    }
    return target;
}

演示: http://jsfiddle.net/4nfWp/1/

您可以这样称呼它:mergeJson({}, object1, object2, object3);(带有超过1号的任意数量的参数)

And you call it like: mergeJson({}, object1, object2, object3); (with any number of arguments past the 1st)

在演示中,实际的调用位于Javascript的底部,因为我需要先定义"JSON",而且它们很长.

In the demo, the actual call is at the bottom of the Javascript because I need the "JSON" to be defined first, and they're long.

更新:

如果使用underscorejs,则可以使用以下内容:

If using underscorejs, you can use something like:

var finalJson = {};
_.each(_.keys(json0,json1), function(key) {
    finalJson[key] = _.flatten(_.zip(json0[key], json1[key]));
});
console.log(finalJson);

演示:: http://jsfiddle.net/4nfWp/4/

信贷去 @djKianoosh 弄清楚:)

这篇关于按键合并JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆