【问题标题】:JSON, Key-Value-Pairs in Groups to "flat" key-value-pairJSON,组中的键值对到“扁平”键值对
【发布时间】:2021-11-12 11:43:25
【问题描述】:

json 的每个键都应与_(或任何有效的 json 符号)结合。拥有一个简单的键值列表。

我有以下结构。一些 json 组(没有数组),在组中有键值对。我需要将它们扁平化为一个键值列表。我试过jq,但只有某事。 “嵌套”/“未嵌套”。我没找到。关于压平或组合键。

所以应该是"key_subkey_subsubkey": "value"

{
    "welcome": {
        "title" : "Hello World"
    },
    "block1": {
        "header": "My Header",
        "body": "My BODY of block 1",
        "footer": "My Footer"
    },
    "multi": {
        "level-01-A": {
            "head": "Head Section",
            "foot": "Foot Section"
            "level-02-A": {
                "head": "Head Section Level 2 A",
                "fead": "Foot Section Level 2 A"
            },
            "level-02-B": {
                "head": "Head Section Level 2 B",
                "fead": "Foot Section Level 2 B"
            },
        },
        "level-01-B": {
            "head": "Head Section",
            "foot": "Foot Section"
        }
        "no-level" : "Foo Bar",     
    }
}

我想拥有

{
    "welcome_title" : "Hello World",

    "block1_header": "My Header",
    "block1_body": "My BODY of block 1",
    "block1_footer": "My Footer",

    "multi_level-01-A_head": "Head Section",
    "multi_level-01-A_foot": "Foot Section",
        
    "multi_level-01-A_level-02-A_head": "Head Section Level 1 A",
    "multi_level-01-A_level-02-A_fead": "Foot Section Level 1 A",

    "multi_level-01-A_level-02-B_head": "Head Section Level 1 B",
    "multi_level-01-A_level-02-B_fead": "Foot Section Level 1 B",

    "multi_level-01-B_head": "Head Section",
    "multi_level-01-B_foot": "Foot Section",

    "multi_no-level" : "Foo Bar"
}

任何想法,我可以使用什么工具?

【问题讨论】:

    标签: json jq


    【解决方案1】:
    [ paths(scalars) as $p | { "key": $p | join("_"), "value": getpath($p) } ] | from_entries
    

    会生成

    {
      "welcome_title": "Hello World",
      "block1_header": "My Header",
      "block1_body": "My BODY of block 1",
      "block1_footer": "My Footer",
      "multi_level-01-A_head": "Head Section",
      "multi_level-01-A_foot": "Foot Section",
      "multi_level-01-A_level-02-A_head": "Head Section Level 2 A",
      "multi_level-01-A_level-02-A_fead": "Foot Section Level 2 A",
      "multi_level-01-A_level-02-B_head": "Head Section Level 2 B",
      "multi_level-01-A_level-02-B_fead": "Foot Section Level 2 B",
      "multi_level-01-B_head": "Head Section",
      "multi_level-01-B_foot": "Foot Section",
      "multi_no-level": "Foo Bar"
    }
    

    你可以测试in this online demo


    paths [docs]

    paths 输出其输入中所有元素的路径(除了它不输出空列表,代表 . 本身)。


    getpath [docs]

    内置函数 getpath 输出 .在 PATHS 中的每个路径中找到。


    from_entries [docs]

    这些函数在对象和键值对数组之间进行转换。如果给 to_entries 传递了一个对象,那么对于输入中的每个 k:v 条目,输出数组包括 {"key": k, "value": v}。


    所以我采取的步骤是:

    1. 获取每个可用(嵌套)路径并将其保存在变量中
      paths(scalars) as $p
      
    2. 创建一个包含keyvalue 的对象。我们可以使用getpath 检索值
      { "key": $p | join("_"), "value": getpath($p) }
      
    3. 使用from_entries 转换为单个对象
      | from_entries
      

    【讨论】:

    • 最好使用paths(scalars) 而不是leaf_paths,因为它会因为被弃用而被删除。
    • 谢谢,速度很快。 leaf_pathsfrom_entries 是什么?它们中的哪一个是我的 json 输入?当我的 json 在我的 home-dir 中,名称为 input.json(例如 ~/input.json)
    • 引用文档时正要按下保存按钮,谢谢提示!
    • 整个命令是cat ~/input.json | jq '[ paths(scalars) as $p | { "key": $p | join("_"), "value": getpath($p) } ] | from_entries'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2017-07-30
    • 2019-04-28
    相关资源
    最近更新 更多