JQ:PigLatin 风格的 FLATTEN 函数
JQ: PigLatin style FLATTEN function
我一直在寻找 jq
中可能不存在的非常具体的功能。如果您知道它不存在,我将不胜感激您的通知和一些解决此问题的建议。
我正在处理 public dataset。我设法将数据减少为以下行格式:
[field1,field2,field3,[author1,...,authorN],[author_type1,...,author_typeN]]
我用来获取此格式的 bash 命令如下:
find aps-dataset-metadata_subdir_path/ -name '*.json' | \
xargs cat | \
jq --compact-output \
'select(.authors != null) | [.identifiers.doi, .date, .journal.id, [.authors[].name], [.authors[].type]]'
请注意 authorN
和 author_typeN
在原始数据中位于同一对象中(即具有相同的父对象)。
我一直在寻找一种方法来从每一行中生成以下内容:
[field1,field2,field3,author1,author_type1]
[field1,field2,field3,author2,author_type2]
...
...
[field1,field2,field3,authorN,author_typeN]
jq
中的 flatten 函数似乎在不生成新列表的情况下进行水平展平。如果你们中的一些人知道 PigLatin,我想要的正是 PigLatin built-in Flatten 运算符。
同样,我知道它可能未在 jq
中实现。在那种情况下,我可能会 post 处理 Python
中的输出,或者你们在答案中建议的任何其他很棒的方式。
非常感谢!
与其在单独的表达式中分别遍历作者,不如遍历作者一次。您可以将结果放在一个变量中,稍后再访问它们。
select(.authors != null) | .authors[] as $author |
[ .identifiers.doi, .date, .journal.id, $author.name, $author.type ]
Jeff 关于使用一步法的建议是有道理的,但如果确实必须将数组 [field1,field2,field3,[author1,...,authorN],[author_type1,...,author_typeN]]
转换为 [field1,field2,field3,authorI,author_typeI]
形式的数组流,那么合适的 jq 过滤器将是:
.[0:2] + ([.[3], .[4]] | transpose[])
我一直在寻找 jq
中可能不存在的非常具体的功能。如果您知道它不存在,我将不胜感激您的通知和一些解决此问题的建议。
我正在处理 public dataset。我设法将数据减少为以下行格式:
[field1,field2,field3,[author1,...,authorN],[author_type1,...,author_typeN]]
我用来获取此格式的 bash 命令如下:
find aps-dataset-metadata_subdir_path/ -name '*.json' | \
xargs cat | \
jq --compact-output \
'select(.authors != null) | [.identifiers.doi, .date, .journal.id, [.authors[].name], [.authors[].type]]'
请注意 authorN
和 author_typeN
在原始数据中位于同一对象中(即具有相同的父对象)。
我一直在寻找一种方法来从每一行中生成以下内容:
[field1,field2,field3,author1,author_type1]
[field1,field2,field3,author2,author_type2]
...
...
[field1,field2,field3,authorN,author_typeN]
jq
中的 flatten 函数似乎在不生成新列表的情况下进行水平展平。如果你们中的一些人知道 PigLatin,我想要的正是 PigLatin built-in Flatten 运算符。
同样,我知道它可能未在 jq
中实现。在那种情况下,我可能会 post 处理 Python
中的输出,或者你们在答案中建议的任何其他很棒的方式。
非常感谢!
与其在单独的表达式中分别遍历作者,不如遍历作者一次。您可以将结果放在一个变量中,稍后再访问它们。
select(.authors != null) | .authors[] as $author |
[ .identifiers.doi, .date, .journal.id, $author.name, $author.type ]
Jeff 关于使用一步法的建议是有道理的,但如果确实必须将数组 [field1,field2,field3,[author1,...,authorN],[author_type1,...,author_typeN]]
转换为 [field1,field2,field3,authorI,author_typeI]
形式的数组流,那么合适的 jq 过滤器将是:
.[0:2] + ([.[3], .[4]] | transpose[])