51793


このようなjsonがあった時に、

[code]

    {
      "taro": {
        "favorite": {
          "fruits": [
            "orange"
          ]
        }
      },"jiro": {
        "favorite": {
          "fruits": [
            "apple",
            "banana",
            "orange"
          ]
        }
      }
    }
    
[/code]

jqで以下のようなjson配列を作り直すには、どのようなコマンドを打てば良いでしょうか。

[code]

    {
      "name": "taro",
      "favorite_fruit":[
        "orange"
     ]
    },{
      "name": "jiro",
      "favorite_fruit": [
        "apple",
        "banana",
        "orange"
     ]
    }
    
[/code]


CC BY-SA 4.0
2019-01-06T12:57:29.530
51759
2019-01-07T09:25:10.807
13492
question
jq
jqでjsonからjson配列を作り直す


`to_entries`から`*`演算子で再起マージが使えるんでないかと。

[code]

    $ <test.json jq '[to_entries[] | {name: .key} * {favorite_fruit: .value.favorite.fruits}]'
    [
      {
        "name": "taro",
        "favorite_fruit": [
          "orange"
        ]
      },
      {
        "name": "jiro",
        "favorite_fruit": [
          "apple",
          "banana",
          "orange"
        ]
      }
    ]
    
[/code]


CC BY-SA 4.0
2019-01-07T09:25:10.807
51793
2019-01-07T09:25:10.807
62
51759
answer