简体   繁体   English

我可以根据列表中的值 output boolean 吗?

[英]Can I output boolean based on values in a list?

Edit: I used the solution provided by @peak to do the following:编辑:我使用@peak提供的解决方案来执行以下操作:

$ jq -r --argjson whitelist '["role1", "role2"]' '
select(has("roles") and any(.roles[]; . == "role1" or . == "role2"))
| (reduce ."roles"[] as $r ({}; .[$r]=true)) as $roles
| [.email, .username, .given_name, .family_name, ($roles[$whitelist[]]
| . != null)]
| @csv
' users.json

Added the select() to filter out users who haven't onboarded yet and don't have any roles, and to ensure the users included in the output have at least one of the target roles.新增select()筛选出尚未入职且没有任何角色的用户,并确保 output 中包含的用户至少具有目标角色之一。

Scenario: user profiles as JSON docs, where each profile has a list object with their assigned roles.场景:用户配置文件为 JSON 文档,其中每个配置文件都有一个列表 object 及其分配的角色。 Example:例子:

{
  "username": "janedoe",
  "roles": [
    "role1",
    "role4",
    "role5"
  ]
}

The actual data file is an ndjson file, one user object as above per line.实际数据文件是一个 ndjson 文件,每行一个用户 object 如上所述。

I am only interested in specific roles, say role1 , role3 , and role4 .我只对特定角色感兴趣,比如role1role3role4 I want to produce a CSV formatted as:我想生成一个 CSV 格式为:

username,role1?,role3?,role4?

eg,例如,

janedoe,true,false,true

The part I haven't figured out is how to output booleans or Y / N in response to the values in the list object.我还没有弄清楚的部分是如何 output 布尔值或 Y / N 以响应列表 object 中的值。 Is this something I can do in jq itself?这是我可以在jq本身做的事情吗?

With your input, the invocation:根据您的输入,调用:

jq -r --argjson whitelist '["role1", "role3", "role4"]' '
  (["username"] + $whitelist),
  [.username, ($whitelist[] as $w | .roles | index([$w]) != null)]
  | @csv
'

produces:产生:

"username","role1","role3","role4"
"janedoe",true,false,true

Notes:笔记:

  1. The second last line of the jq filter above could be shortened to:上面 jq 过滤器的倒数第二行可以缩短为:

    [.username, (.roles | index($whitelist[]) != null)]

  2. Presumably if there were more than one user, you'd only want the header row once, in which case the above solution would need to be tweaked.据推测,如果有多个用户,您只需要 header 行一次,在这种情况下,需要调整上述解决方案。

Using IN/1使用 IN/1

Because index/1 is not as efficient as it might be, you might like to consider this alternative:因为index/1没有它可能的效率,你可能想考虑这个替代方案:

  (["username"] + $whitelist),
  (.roles as $roles | [.username, ($whitelist[] | IN($roles[]) )])
  | @csv

Using a JSON dictionary使用 JSON 字典

If the number of roles was very large, then it would probably be more efficient to construct a JSON dictionary to avoid repeated linear lookups:如果角色的数量非常多,那么构造 JSON 字典以避免重复的线性查找可能会更有效:

  (reduce .roles[] as $r ({}; .[$r]=true)) as $roles
  | (["username"] + $whitelist),
    [.username, ($roles[$whitelist[]] != null)]
  | @csv

With ndjson as input使用 ndjson 作为输入

For efficiency, and to ensure there's just one header, you could use inputs with the -n command-line option.为了提高效率并确保只有一个 header,您可以使用带有 -n 命令行选项的inputs Adding the extra fields mentioned in the revised Q, you might end up with:添加修改后的 Q 中提到的额外字段,您最终可能会得到:

jq -nr --argjson whitelist '["role1", "role2"]' '
  ["email", "username", "given_name", "family_name"] as $greenlist
  | ($greenlist + $whitelist),
    (inputs 
     | select(has("roles") and any(.roles[] == $whitelist[]; true))
     | (reduce ."roles"[] as $r ({}; .[$r]=true)) as $roles
     | [ .[$greenlist[]], ($roles[$whitelist[]] != null) ])
  | @csv
' users.json

暂无
暂无

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

相关问题 如何使用XMLout转换JSON布尔值以进行输出? - How can I convert JSON boolean values for output using XMLout? 如何获取动态TClientDataset(基于TRESTResponseDataSetAdapter)以正确识别布尔值? - How do I get a dynamic TClientDataset (based on TRESTResponseDataSetAdapter) to correctly recognise boolean values? 我有一个包含不同值列表的 json 文件。 我想根据值给出不同颜色的不同值范围。 我怎样才能做到这一点? - I have a json file with list of different values. I want to give different ranges of values different color on based on the value. How can I do that? 我可以基于输入中的嵌套属性使用jq输出属性吗? - Can I output a property with jq based on a nested property in the input? boolean 值可以/应该用引号在 json 中传递吗? - Can/should boolean values be passed in json with quotes? 如何在 JSON 文件中重复保存 python output 值 - How can i repeatedly save the python output values in a JSON file 如何将输出值的顺序从 JSON 更改为 FlatList? - How can I change the sequence of output values from JSON to FlatList? 如何列出值并使用此 json 的值生成变量? (在 PHP 中) - How can I list the values & make variable with the values of this json? (In PHP) 如何将这个json日志输出解析到我的bean列表中? - How can I parse this json log output into a list of my bean? 如何让NSJSONSerialization输出布尔值为true或false? - How do I get NSJSONSerialization to output a boolean as true or false?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM