jq コマンドで CSV を JSON形式のデータに変換

2023年2月12日

簡単なサンプル

type in.csv | jq-win64.exe -R -f mapping.jq>out.json

mapping.jq

split(“,")|
{“KeyType":.[0],
“DisplayText":.[1],
“FontSize":.[2],
“Position": {
“X": .[3],
“Y": .[4],
“Width": .[5],
“Height": .[6]
},

}

in.csv

PageUp,ページ↑,12,0,100,50,50
PageDown,ページ↓,12,0,150,50,50

out.json

   {
      "KeyType": "PageUp",
      "DisplayText": "ページ↑",
      "FontSize": "12",
      "Position": {
        "X": "0",
        "Y": "100",
        "Width": "50",
        "Height": "50"
      }
    }
    {
      "KeyType": "PageDown",
      "DisplayText": "ページ↓",
      "FontSize": "12",
      "Position": {
        "X": "0",
        "Y": "150",
        "Width": "50",
        "Height": "50"
      }
    }

CSV ファイルはUTF 8でBom なし保存。。。なぜか出力ファイルにカンマが入りません

「ちーたんタッチボード」の設定ファイルを作るサンプルです

「ちーたんタッチボード」

https://sorceryforce.net/ja/tiitan-touch-board

set outf=a.json
set mappingi=mappingi
set mappingt=mappingt
set mappingp=mappingp
set Width=50
set Height=300

echo { > %outf%
echo  "IdentityName": "171_tate_1_1_01", >> %outf%
echo  "Author": "eightban", >> %outf%
echo  "Version": 1, >> %outf%
echo  "KeyReleaseTimingWhenActiveWindowChange": "Window", >> %outf%
echo  "GeneratorIdentityName": "none", >> %outf%
echo  "GeneratorVersion": 1.0, >> %outf%
echo  "Boards": [ >> %outf%
echo   { >> %outf%
echo    "Keys": [ >> %outf%


type %mappingi%.csv|jq-win64.exe -R -c -f %mappingi%.jq>>%outf%
type %mappingt%.csv|jq-win64.exe -R -c -f %mappingt%.jq>>%outf%
type %mappingp%.csv|jq-win64.exe -R -c -f %mappingp%.jq>>%outf%
echo   {} >> %outf%

echo    ], >> %outf%
echo    "IdentityName": "tate_1", >> %outf%
echo    "Position": { >> %outf%
echo     "X": 0, >> %outf%
echo     "Y": 0, >> %outf%
echo     "Width": %Width%, >> %outf%
echo     "Height": %Height% >> %outf%
echo    }, >> %outf%
echo    "StartPositionType": "TopLeft", >> %outf%
echo    "PositionOffset": { >> %outf%
echo     "X": 0, >> %outf%
echo     "Y": 0 >> %outf%
echo    }, >> %outf%
echo    "CenterPosition": { >> %outf%
echo     "X": 0, >> %outf%
echo     "Y": 0 >> %outf%
echo    }, >> %outf%
echo    "FontSizeScale": 0.9, >> %outf%
echo    "BoardScale": 1 >> %outf%
echo   } >> %outf%
echo  ] >> %outf%
echo } >> %outf%
powershell -Command "(Get-Content -Path '%outf%' -Encoding utf8 ) -replace '}}','}},'   | Out-File '%outf%.wk' -Encoding utf8 "
powershell -Command "(Get-Content -Path '%outf%.wk' -Encoding utf8 ) -replace '}]}','}]},'   | Out-File '%outf%' -Encoding utf8 "

type %outf%|jq-win64.exe >%outf%.wk
type %outf%.wk>%outf%
del %outf%.wk
powershell でカンマを入れています。最後に見やすいように整形しています。

mappingi.jq

split(“,")|
{“KeyType":.[0],
“ImageName":.[1],
“ImageStretchMode":.[2],
“Position": {
“X": .[3] | tonumber,
“Y": .[4] | tonumber,
“Width": .[5] | tonumber,
“Height": .[6] | tonumber
},
}

mappingi.csv

BoardMove,BoardMove,Uniform,0,0,50,50
ConfigMenu,Config,Uniform,0,50,50,50

mappingt.jq

split(“,")|
{“KeyType":.[0],
“DisplayText":.[1],
“FontSize":.[2],
“Position": {
“X": .[3],
“Y": .[4],
“Width": .[5],
“Height": .[6]
},

}

mappingt.csv

PageUp,ページ↑,12,0,100,50,50
PageDown,ページ↓,12,0,150,50,50

mappingp.jq

split(“,")|
{“KeyType":.[0],
“DisplayText":.[1],
“FontSize":.[2],
“Position": {
“X": .[3],
“Y": .[4],
“Width": .[5],
“Height": .[6]
},
“Processes": [
{
“KeyType": .[7]
},
{
“KeyType": .[8]
}
]
}

mappingp.csv

CtrlC,コピー,12,0,200,50,50,LeftCtrl,C
CtrlV,貼付,12,0,250,50,50,LeftCtrl,V

結果

{
  "IdentityName": "171_tate_1_1_01",
  "Author": "eightban",
  "Version": 1,
  "KeyReleaseTimingWhenActiveWindowChange": "Window",
  "GeneratorIdentityName": "none",
  "GeneratorVersion": 1,
  "Boards": [
    {
      "Keys": [
        {
          "KeyType": "BoardMove",
          "ImageName": "BoardMove",
          "ImageStretchMode": "UniformToFill",
          "Position": {
            "X": 0,
            "Y": 0,
            "Width": 50,
            "Height": 50
          }
        },
        {
          "KeyType": "ConfigMenu",
          "ImageName": "Config",
          "ImageStretchMode": "UniformToFill",
          "Position": {
            "X": 0,
            "Y": 50,
            "Width": 50,
            "Height": 50
          }
        },
        {
          "KeyType": "PageUp",
          "DisplayText": "ページ↑",
          "FontSize": "12",
          "Position": {
            "X": "0",
            "Y": "100",
            "Width": "50",
            "Height": "50"
          }
        },
        {
          "KeyType": "PageDown",
          "DisplayText": "ページ↓",
          "FontSize": "12",
          "Position": {
            "X": "0",
            "Y": "150",
            "Width": "50",
            "Height": "50"
          }
        },
        {
          "KeyType": "CtrlC",
          "DisplayText": "コピー",
          "FontSize": "12",
          "Position": {
            "X": "0",
            "Y": "200",
            "Width": "50",
            "Height": "50"
          },
          "Processes": [
            {
              "KeyType": "LeftCtrl"
            },
            {
              "KeyType": "C"
            }
          ]
        },
        {
          "KeyType": "CtrlV",
          "DisplayText": "貼付",
          "FontSize": "12",
          "Position": {
            "X": "0",
            "Y": "250",
            "Width": "50",
            "Height": "50"
          },
          "Processes": [
            {
              "KeyType": "LeftCtrl"
            },
            {
              "KeyType": "V"
            }
          ]
        },
        {}
      ],
      "IdentityName": "tate_1",
      "Position": {
        "X": 0,
        "Y": 0,
        "Width": 50,
        "Height": 300
      },
      "StartPositionType": "TopLeft",
      "PositionOffset": {
        "X": 0,
        "Y": 0
      },
      "CenterPosition": {
        "X": 0,
        "Y": 0
      },
      "FontSizeScale": 0.9,
      "BoardScale": 1
    }
  ]
}

jqコマンド

Posted by eightban