2017-07-31 74 views
-1

我实际上正在寻找一种解决方案来基于几个“索引”键合并几个(3-4)数组。JQ:在索引键上合并几个json数组

例阵列1:

{"Fan":[{ "Last Name":Mueller,"Firstname":Martin,"Adress":Madisson Square,"City":"New York","DegreesIndex":3,"SchoolIndex2":1,}] 

DegreesIndex和Schoolindex是指两个不同的密钥中另外两个阵列:

例如阵列2:

{"Degrees":[ 
{"DegreesIndex":3, 
"Key":"12759303, 
"Degrees":1.6}]} 

例如阵列3:

{“School”:[ {“SchoolIndex”:1, “Teaser”:“12759303.8, ”Trainer“:Miller}]}

如何在windows10下基于”index“键与JQ 1.5合并数组?

问候 蒂莫

+1

1.请修复JSON。 2.请在http://stackoverflow.com/help/mcve上查看指导原则后澄清问题 – peak

回答

1

我认为这可能是接近你在找什么:

# input: an object 
def merge_by_index(obj; ix): 
    ix as $index 
    | . + (obj | map(select(ix == $index)) [0]) 
    | del(ix) ; 

清理你的样品输入后,并假设A1,A2和A3是 3顶层对象:

a1 
| .Fan[0] |= merge_by_index(a2|.Degrees; .DegreesIndex) 
| .Fan[0] |= merge_by_index(a3|.School; .SchoolIndex) 

生产:

{ 
    "Fan": [ 
    { 
     "Last Name": "Mueller", 
     "Firstname": "Martin", 
     "Adress": "Madisson Square", 
     "City": "New York", 
     "Key": 12759303, 
     "Degrees": 1.6, 
     "Teaser": 12759303.8, 
     "Trainer": "Miller" 
    } 
    ] 
} 
0

下面是我将如何处理这个问题。这是更详细一点,然后peak的方法。

首先用数据定义一些数组。请注意,我冒昧添加另一个条目来测试当没有匹配的DegreesIndexSchoolIndex对于给定的Fan。我喜欢在这里使用函数,因为您可以轻松地用您需要的任何逻辑来替换这些函数以获取真实数据。

def array1: { 
    "Fan": [ 
    { 
     "Last Name":"Mueller", "First Name":"Martin", 
     "Address": "Madisson Square", "City": "New York", 
     "DegreesIndex": 3, "SchoolIndex": 1 
    }, 
    { 
     "Last Name":"Roberts", "First Name":"Bob", 
     "DegreesIndex": 2, "SchoolIndex": 4 
    }, 
    { 
     "Last Name":"Skywalker", "First Name":"Luke", 
     "DegreesIndex": 5, "SchoolIndex": 1 
    } 
    ]}; 

def array2: { 
"Degrees": [ 
    { "DegreesIndex":3, "Key": "12759303", "Degrees":1.6 }, 
    { "DegreesIndex":2, "Key": "2",  "Degrees":2 } 
    ]}; 

def array3: { 
    "School": [ 
    { "SchoolIndex":1, "Teaser":"12759303.8", "Trainer":"Miller" }, 
    { "SchoolIndex":2, "Teaser":"2",   "Trainer":"Miller" } 
    ]}; 

现在定义一些简单的查找函数,它将返回与指定键匹配的记录。请注意,如果找不到项目,而不是导致完全删除扇区,则使用[[0]构造来返回null。

def LookupDegrees($i): 
    [ 
     array2 
    | .Degrees[] 
    | select(.DegreesIndex == $i) 
    ][0] 
; 

def LookupSchool($i): 
    [ 
     array3 
    | .School[] 
    | select(.SchoolIndex == $i) 
    ][0] 
; 

所有这一切使得主程序简单:

array1 
| .Fan[] 
| .Degrees = LookupDegrees(.DegreesIndex) 
| .School = LookupSchool(.SchoolIndex) 

下面是结果我收到的时候我用JQ -n -f file.jq运行

{ 
    "Last Name": "Mueller", 
    "First Name": "Martin", 
    "Address": "Madisson Square", 
    "City": "New York", 
    "DegreesIndex": 3, 
    "SchoolIndex": 1, 
    "Degrees": { 
    "DegreesIndex": 3, 
    "Key": "12759303", 
    "Degrees": 1.6 
    }, 
    "School": { 
    "SchoolIndex": 1, 
    "Teaser": "12759303.8", 
    "Trainer": "Miller" 
    } 
} 
{ 
    "Last Name": "Roberts", 
    "First Name": "Bob", 
    "DegreesIndex": 2, 
    "SchoolIndex": 4, 
    "Degrees": { 
    "DegreesIndex": 2, 
    "Key": "2", 
    "Degrees": 2 
    }, 
    "School": null 
} 
{ 
    "Last Name": "Skywalker", 
    "First Name": "Luke", 
    "DegreesIndex": 5, 
    "SchoolIndex": 1, 
    "Degrees": null, 
    "School": { 
    "SchoolIndex": 1, 
    "Teaser": "12759303.8", 
    "Trainer": "Miller" 
    } 
} 

这应该如果您需要不同的嵌套或输出,很容易调整。