Help us understand the problem. What is going on with this article?

小ネタ: JSONからYAMLにRubyワンライナーで変換する

More than 1 year has passed since last update.

解決したい課題

JSONをカジュアルにYAMLに変換したい。
具体的には、Chef付属の情報収集ツールohaiを実行したときに出力されるJSONを、YAMLに変換します。

実装例

特になんの説明もなく。
以下が…

$ ohai
{
  "cpu": {
    "0": {
      "vendor_id": "GenuineIntel",
      "family": "6",
      "model": "61",
      "model_name": "Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz",
      "stepping": "4",
      "mhz": "1599.999",
      "cache_size": "3072 KB",
      "physical_id": "0",
      "core_id": "0",
      "cores": "1",
      "flags": [
        "fpu",
        "vme",
        "de",
        "pse",
(以下略)

こうすることで…

$ ohai | /opt/chef/embedded/bin/ruby -ryaml -rjson -e 'puts YAML.dump(JSON.parse(STDIN.read))'

こうなります。

$ ohai | /opt/chef/embedded/bin/ruby -ryaml -rjson -e 'puts YAML.dump(JSON.parse(STDIN.read))'
---
cpu:
  '0':
    vendor_id: GenuineIntel
    family: '6'
    model: '61'
    model_name: Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz
    stepping: '4'
    mhz: '1599.999'
    cache_size: 3072 KB
    physical_id: '0'
    core_id: '0'
    cores: '1'
    flags:
    - fpu
    - vme
    - de
    - pse
(以下略)

補足

小ネタでした。

kentarok
投稿内容は私自身の見解であり、所属する団体・組織の立場、戦略、意見を代表するものではありません
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away