jq-流过滤相同键的多个值 [英] jq --stream filter on multiple values of same key
问题描述
我正在处理非常大的JSON,其中我需要使用键的值过滤内部JSON对象.我的JSON如下所示:
I am processing a very large JSON wherein I need to filter the inner JSON objects using a value of a key. My JSON looks like as follows:
{"userActivities":{"L3ATRosRdbDgSmX75Z":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday","localDate":"2018-01-20"},"L3ATSFGrpAYRkIIKqrh":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday","localDate":"2018-01-21"},"L3AVHvmReBBPNGluvHl":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday","localDate":"2018-01-22"},"L3AVIcqaDpZxLf6ispK":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday,"localDate":"2018-01-19"}}}
我想对localDate值进行过滤,例如2018-01-20中的localDate或"2018-01-21"中的localDate,以使输出看起来像这样.
I want to put a filter on localDate values such that localDate in 2018-01-20 or localDate in "2018-01-21" such that the output look like.
{"userActivities":{"L3ATRosRdbDgSmX75Z":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday","localDate":"2018-01-20"},"L3ATSFGrpAYRkIIKqrh":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday","localDate":"2018-01-21"}}}
我在这里问过类似的问题,意识到我需要对多个值进行过滤,并保留JSON的原始结构.
I have asked a similar question here and realised that I need to put filter on multiple values and retain the original structure of JSON.
https://stackoverflow.com/questions/52324497/how-to-filter-json-using-jq-stream
提前感谢一吨!
推荐答案
来自 jq Cookbook ,让我们借用def atomize(s)
:
# Convert an object (presented in streaming form as the stream s) into
# a stream of single-key objects
# Examples:
# atomize({a:1,b:2}|tostream)
# atomize(inputs) (used in conjunction with "jq -n --stream")
def atomize(s):
fromstream(foreach s as $in ( {previous:null, emit: null};
if ($in | length == 2) and ($in|.[0][0]) != .previous and .previous != null
then {emit: [[.previous]], previous: $in|.[0][0]}
else { previous: ($in|.[0][0]), emit: null}
end;
(.emit // empty), $in) ) ;
由于OP描述的顶级对象仅包含一个键,因此我们可以按以下方式选择2018年8月的对象:
Since the top-level object described by the OP contains just one key, we can select the August 2018 objects as follows:
atomize(1|truncate_stream(inputs))
| select( .[].localDate[0:7] == "2018-08")
如果要将这些对象收集到复合对象中,则可能需要注意内存,因此您可能希望将选定的对象通过管道传输到另一个程序(例如awk或jq).否则,我会选择:
If you want these collected into a composite object, you might have to be careful about memory, so you might want to pipe the selected objects to another program (e.g. awk or jq). Otherwise, I'd go with:
def add(s): reduce s as $x (null; .+$x);
{"userActivities": add(
atomize(1|truncate_stream(inputs | select(.[0][0] == "userActivities")))
| select( .[].localDate[0:7] =="2018-01") ) }
变化
如果顶级对象具有多个键,则以下变体将是合适的:
Variation
If the top-level object has more than one key, then the following variation would be appropriate:
atomize(1|truncate_stream(inputs | select(.[0][0] == "userActivities")))
| select( .[].localDate[0:7] =="2018-08")
这篇关于jq-流过滤相同键的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!