0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コマンドラインツールの jq をつかって、オブジェクトの配列をパースしたいと思っています。オブジェクトの文字列属性がjson文字列としてvalidだった場合はそれをfromjsonでパースし、そうでない場合はそのまま出力するjqスクリプトを書いてください。

0
Posted at

以下のようなjqスクリプトを使うことで、オブジェクトの配列をパースし、文字列属性がJSON文字列としてvalidな場合はパースして、そうでない場合はそのまま出力することができます。

.[]
  | with_entries(if (.value | type == "string") and (.value | fromjson? | type) == "object" then .value |= fromjson else . end)

このスクリプトでは、配列の各要素について、 with_entries 関数を使用して、各エントリーを処理します。そして、文字列属性がJSON文字列としてvalidな場合は fromjson 関数を使用してパースし、そうでない場合はオブジェクトのまま出力されます。最終的に、このスクリプトは、パースされた配列を出力します。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?