简体   繁体   English

如何过滤掉具有特定属性的元素(或保留没有该属性的元素)

[英]How to filter out elements with a particular property (or keep elements without that property)

I'm processing the output from dcm2json , which converts the metadata from medical imaging data in the DICOM format to JSON.我正在处理来自 dcm2json 的output ,它将元数据从 DICOM 格式的医学成像数据转换为 JSON。 The values for this metadata is mostly strings, integers, floats, and similar, but it also includes inline binary values in the form of base64-encoded strings.此元数据的值主要是字符串、整数、浮点数等,但它也包括 base64 编码字符串形式的内联二进制值。 We don't need those binaries and they can get pretty large, so I need to filter out all metadata elements that have an InlineBinary property.我们不需要这些二进制文件,它们会变得非常大,所以我需要过滤掉所有具有 InlineBinary 属性的元数据元素。 Below is a (very simple small) sample of the JSON output from dcm2json:下面是来自 dcm2json 的 JSON output 的(非常简单的小)示例:

{
    "00080005": {
        "vr": "CS",
        "Value": ["ISO_IR 192"]
    },
    "00291010": {
        "vr": "OB",
        "InlineBinary": "Zm9vYmFyCg=="
    }
}

I want to transform this to:我想将其转换为:

{
    "00080005": {
        "vr": "CS",
        "Value": ["ISO_IR 192"]
    }
}

I tried a few different things that didn't work but finally ended up using this:我尝试了一些没有用的不同方法,但最终还是使用了这个:

$ dcm2json file.dcm | jq '[to_entries | .[] | select(.value.Value)] | from_entries'

I kept playing with it though because I don't like having that expression embedded in the array (ie [to_entries...] ).不过我一直在玩它,因为我不喜欢将该表达式嵌入到数组中(即[to_entries...] )。 I came up with something a bit more elegant, but I'm totally stumped as to why it works the way it does:我想出了一些更优雅的东西,但我完全不知道为什么它会这样工作:

jq 'to_entries | . - map(select(.value | has("InlineBinary") == true)) | from_entries' | less

What's confusing is the has("InlineBinary") == true bit.令人困惑的是has("InlineBinary") == true位。 I first ran this comparing it to false because what I wanted was those elements that don't have the InlineBinary property.我首先将它与false进行比较,因为我想要的是那些没有InlineBinary属性的元素。 Why does it work seemingly opposite what I think I'm requesting?为什么它看起来与我认为我要求的相反? Given that I really don't understand what's happening with the . - map(...)鉴于我真的不明白. - map(...) . - map(...) structure in there (I totally swiped it from another post where someone asked a similar question), I'm not surprised it does something I don't understand but I'd like to understand why that is:) . - map(...)结构(我完全从另一篇有人问过类似问题的帖子中刷了它),我并不惊讶它做了我不明白的事情,但我想了解为什么会这样: )

The other thing I'm confused about is to_entries/from_entries/with_entries .我感到困惑的另一件事是to_entries/from_entries/with_entries The manual says about these :手册说明了这些

with_entries(foo) is a shorthand for to_entries | with_entries(foo) 是 to_entries 的简写 | map(foo) |地图(foo) | from_entries from_entries

Cool: So that would be:酷:所以那将是:

jq 'with_entries(map( . - map(select(.value | has("InlineBinary") == true))))'

But that doesn't work:但这不起作用:

$ cat 1.json | jq 'with_entries(map(. - map(select(.value | has("InlineBinary") == true))))'
jq: error (at <stdin>:848): Cannot iterate over string ("00080005")

Given that this statement is supposed to be functionally equivalent, I'm not sure why this wouldn't work.鉴于此语句应该在功能上等效,我不确定为什么这不起作用。

Thanks for any info you can provide!感谢您提供的任何信息!

When selecting key-value pairs, with_entries is often the tool of choice:选择键值对时, with_entries通常是首选工具:

with_entries( select(.value | has("InlineBinary") | not) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM