配列を除くすべてのキーのリスト

配列を除くすべてのキーのリスト

数値、文字列、null、ブール値などのデータを含むすべてのキーをダンプするために、すべてのjqコマンドに対して単一のサイズを生成しようとしています。

配列値に達したら、配列名を印刷するのではなく、配列内を調べて配列内からキー名を抽出する必要があります。

サンプル出力は次のとおりです...

coord.lon
coord.lat
weather.0
weather.0.id
weather.0.main
weather.0.description
weather.0.icon
base

動作する次のビットがありますが、配列である親キーを返すので、そのキーは必要なく、サブキーのみが返されます。

jq -r 'paths | map(.|tostring)|join(".")' weather.json

誰でも助けてもらえますか?私は多くの困難を経験しました。

例JSON

  {
  "coord": {
    "lon": -90.85,
    "lat": 38.8
  },
  "weather": [
    {
      "id": 501,
      "main": "Rain",
      "description": "moderate rain",
      "icon": "10d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 38.19,
    "pressure": 1020,
    "humidity": 100,
    "temp_min": 36,
    "temp_max": 39.99
  },
  "visibility": 4023,
  "wind": {
    "speed": 4.7,
    "deg": 330
  },
  "rain": {
    "1h": 1.82
  },
  "clouds": {
    "all": 75
  },
  "dt": 1572448290,
  "sys": {
    "type": 1,
    "id": 4178,
    "country": "US",
    "sunrise": 1572438477,
    "sunset": 1572476760
  },
  "timezone": -18000,
  "id": 0,
  "name": "Wentzville",
  "cod": 200
}

ベストアンサー1

次のように最初に配列を削除できます。

del(.[] | select(type=="array"))

オブジェクトのみを保持するには、次の手順を実行します。

<infile.json jq -r '
del(.[] | select(type!="object")) |
paths                             |
map(tostring)                     |
select(length==2)                 |
join(".")
'

出力:

coord.lon
coord.lat
main.temp
main.pressure
main.humidity
main.temp_min
main.temp_max
wind.speed
wind.deg
rain.1h
clouds.all
sys.type
sys.id
sys.country
sys.sunrise
sys.sunset

編集する

たぶん2つの答えの組み合わせをしたいですか?

paths(scalars)      |
select(length >= 2) |
map(tostring)       |
join(".")

出力:

coord.lon
coord.lat
weather.0.id
weather.0.main
weather.0.description
weather.0.icon
main.temp
main.pressure
main.humidity
main.temp_min
main.temp_max
wind.speed
wind.deg
rain.1h
clouds.all
sys.type
sys.id
sys.country
sys.sunrise
sys.sunset

おすすめ記事