我有两个JSON对象,使用JSON.parse
创建,我想合并和聚合。
我无法将数据存储在Mongo数据库中,并且不清楚如何继续。
第一个JSON文件包含原始数据:
[
{
"sector": {
"url": "http://TestUrl/api/sectors/11110",
"code": "11110",
"name": "Education policy and administrative management"
},
"budget": 5742
},
{
"sector": {
"url": "http://TestUrl/api/sectors/11110",
"code": "11110",
"name": "Education policy and administrative management"
},
"budget": 5620
},
{
"sector": {
"url": "http://TestUrl/api/sectors/12110",
"code": "12110",
"name": "Health policy and administrative management"
},
"budget": 5524
}, ]
第二个JSON文件包含数据合并操作所需的映射:
{
"Code (L3)":11110,
"High Level Code (L1)":1,
"High Level Sector Description":"Education",
"Name":"Education policy and administrative management",
"Description":"Education sector policy, planning and programmes; aid to education ministries, administration and management systems; institution capacity building and advice; school management and governance; curriculum and materials development; unspecified education activities.",
"Category (L2)":111,
"Category Name":"Education, level unspecified",
"Category Description":"The codes in this category are to be used only when level of education is unspecified or unknown (e.g. training of primary school teachers should be coded under 11220)."
},
{
"Code (L3)":12110,
"High Level Code (L1)":2,
"High Level Sector Description":"Health",
"Name":"Health policy and administrative management",
"Description":"Health sector policy, planning and programmes; aid to health ministries, public health administration; institution capacity building and advice; medical insurance programmes; unspecified health activities.",
"Category (L2)":121,
"Category Name":"Health, general",
"Category Description":""
},
{
"Code (L3)":99999,
"High Level Code (L1)":9,
"High Level Sector Description":"Unused Code",
"Name":"Extra Code",
"Description":"Shows Data Issue",
"Category (L2)":998,
"Category Name":"Extra, Code",
"Category Description":""
},
我想使用"代码"来连接两个文件中的数据。第一个文件中的值和"代码(L3)"第二个文件中的值。在SQL术语中,我想做一个"内部连接"使用这些值作为连接点的文件。
然后,我希望汇总第一个文件中的所有预算值,用于"高级代码(L1)"第二个文件中的值以生成以下JSON对象:
{
"High Level Code (L1)":1,
"High Level Sector Description":"Education",
"Budget”: 11362
},
{
"High Level Code (L1)":2,
"High Level Sector Description":"Health",
"Budget”: 5524
}
这对数据库来说是一个非常简单的任务,但我担心这个选项不可用。我们在Sinatra上运行我们的站点,因此我无法使用任何特定于Rails的帮助程序方法。
更新:我现在正在使用输入的真实数据,我发现映射文件中有多个JSON对象,其中包含" Code(L3)"不映射到原始数据文件中任何[Sector] [code]值的值。
我尝试过多种解决方法(将数据分解为2D数组,然后尝试将结果数组作为哈希表返回)但我无法获得任何工作。
我回到了我接受这个问题的答案,因为这是一个非常优雅的解决方案,我不想两次提出同样的问题 - 我只是无法弄清楚如何制作当它们与原始数据文件中的任何内容不匹配时,它会忽略映射文件中的项目。
答案 0 :(得分:1)
这很简单,图像你首先列出的是名字来源,第二个被命名为"值"或者其他什么。我们将通过"值",并提取必填字段,并在其中找到"来源",所需的值:
// ==UserScript==
...............
// @version 2.0.1
...............
// ==/UserScript==
if (GM_getValue("version", "") < "2.0.1") {
GM_setValue("version", "2.0.1");
alert("Updated");
}
相当于&#34;加入&#34;使用&#34; find&#34;来创建数据库。操作。我们遍历源数组以找到与&#34; Code(L3)&#34;相同的扇区/代码值,然后我们提取&#34;预算&#34;我们总结了所有这些提取的值....
结果如下:
values.map do |elem|
{ "High Level Code (L1)" => elem["High Level Code (L1)"],
"High Level Sector Description" => elem["High Level Sector Description"],
"Budget" => sources.select do |source|
source["sector"]["code"] == elem["Code (L3)"].to_s
end.map{|elem|elem["budget"]}.sum
}
end
答案 1 :(得分:0)
如何通过第一个数据集并使用代码作为键将其索引到哈希,然后浏览第二个数据集并从哈希中查找每个键的相应数据。有点蛮力,但.. ..