JSON字段具有相同的名称
在实践中,键必须在JSON对象中是唯一的(例如,JSON语法是否允许在对象中使用重复键?)。 但是,假设我有一个包含以下内容的文件:
{
"a" : "1",
"b" : "2",
"a" : "3"
}
有没有简单的方法将重复键转换为数组? 以便该文件变为:
{
"a" : [ {"key": "1"}, {"key": "3"}],
"b" : "2"
}
或者类似的东西,但是它将重复的键组合成一个数组(或者找到并且提供重复键值的另一种方式)。
以下是Java中的一个解决方案:将具有重复键的JSON对象转换为JSON数组
有没有办法用awk / bash / python来做到这一点?
如果你的输入实际上是一个平原的JSON对象,并且基元作为值,这应该可以工作:
jq -s --stream 'group_by(.[0]) | map({"key": .[0][0][0], "value": map(.[1])}) | from_entries'
{
"a": [
"1",
"3"
],
"b": [
"2"
]
}
对于更复杂的输出,这将需要真正理解 - --stream
应该如何使用,这超出了我的想象。
基于圣地亚哥的回答,使用-s --stream
,下面的过滤器一次构建一个对象,因此保留了特定键的键和值的顺序:
reduce (.[] | select(length==2)) as $kv ({};
$kv[0][0] as $k
|$kv[1] as $v
| (.[$k]|type) as $t
| if $t == "null" then .[$k] = $v
elif $t == "array" then .[$k] += [$v]
else .[$k] = [ .[$k], $v ]
end)
对于给定的输入,结果是:
{
"a": [
"1",
"3"
],
"b": "2"
}
为了说明保留每个键的值的排序,请考虑以下输入:
{
"c" : "C",
"a" : "1",
"b" : "2",
"a" : "3",
"b" : "1"
}
上面的过滤器产生的输出是:
{
"c": "C",
"a": [
"1",
"3"
],
"b": [
"2",
"1"
]
}
根据峰值的答案,下面的过滤器也适用于多对象输入 , 带有嵌套对象并且没有slurp-Option (-s)。
这不是对最初问题的回答,但是因为jq-FAQ链接在这里,它可能对一些访问者有用
文件jqmergekeys.txt
def consumestream($arr): # Reads stream elements from stdin until we have enough elements to build one object and returns them as array
input as $inp
| if $inp|has(1) then consumestream($arr+[$inp]) # input=keyvalue pair => Add to array and consume more
elif ($inp[0]|has(1)) then consumestream($arr) # input=closing subkey => Skip and consume more
else $arr end; # input=closing root object => return array
def convert2obj($stream): # Converts an object in stream notation into an object, and merges the values of duplicate keys into arrays
reduce ($stream[]) as $kv ({}; # This function is based on http://stackoverflow.com/a/36974355/2606757
$kv[0] as $k
| $kv[1] as $v
| (getpath($k)|type) as $t # type of existing value under the given key
| if $t == "null" then setpath($k;$v) # value not existing => set value
elif $t == "array" then setpath($k; getpath($k) + [$v] ) # value is already an array => add value to array
else setpath($k; [getpath($k), $v ]) # single value => put existing and new value into an array
end);
def mainloop(f): (convert2obj(consumestream([input]))|f),mainloop(f); # Consumes streams forever, converts them into an object and applies the user provided filter
def mergeduplicates(f): try mainloop(f) catch if .=="break" then empty else error end; # Catches the "break" thrown by jq if there's no more input
#---------------- User code below --------------------------
mergeduplicates(.) # merge duplicate keys in input, without any additional filters
#mergeduplicates(select(.layers)|.layers.frame) # merge duplicate keys in input and apply some filter afterwards
例:
tshark -T ek | jq -nc --stream -f ./jqmergekeys.txt
链接地址: http://www.djcxy.com/p/37837.html