从.csv中提取变量以构建JSON输出

zyfwsgd6  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(185)

我有一个.csv文件,其中有一个简短的变量列表,我需要从中创建一个.json文件。一切都工作得很好,但其中一个变量($input[5]/“programNumber”)需要作为整数导入,我使用的脚本是用一组引号提取所有内容,这对前4个变量都很好。我只是对导入语法不够熟悉,无法理解它。先谢谢你的帮助。
以下是脚本:

jq -Rsn '
   {"services":
      [inputs
         | . / "\n"
         | (.[] | select(length > 0) | . / ",") as $input
         |   {
               "type": "mpeg2tsUdp",
               "sources":
               [
                  {
                    "address": $input[1],
                    "port": 2000,
                    "sourceAddresses":
                    [
                        $input[3]
                    ],
                    "programSelection":
                    {
                        "type": "PmtProgramNumber",
                        "programNumber": $input[5]
                    }
                  },
                  {
                    "address": $input[2],
                    "port": 2000,
                    "sourceAddresses":
                    [
                        $input[4]
                    ],
                    "programSelection":
                    {
                        "type": "PmtProgramNumber",
                        "programNumber": $input[5]
                    }
                  }
               ],
               "streams": [],
               "id": $input[0]
            }
         ],
         "id": "Inputs",
         "name": "Inputs"
      }
' <SourceList.csv

下面是.csv的内容:

FNCHD,239.194.208.9,239.194.216.9,10.110.208.9,10.110.216.9,23
MTV,239.194.208.216,239.194.216.216,10.110.208.216,10.110.216.216,702
AEHD,239.194.208.6,239.194.216.6,10.110.208.6,10.110.216.6,1
SLATE,239.1.1.1,239.1.2.1,10.100.1.1,10.100.2.1,3

这里是输出:

{
  "services": [
    {
      "type": "mpeg2tsUdp",
      "sources": [
        {
          "address": "239.194.208.9",
          "port": 2000,
          "sourceAddresses": [
            "10.110.208.9"
          ],
          "programSelection": {
            "type": "PmtProgramNumber",
            "programNumber": "23"
          }
        },
        {
          "address": "239.194.216.9",
          "port": 2000,
          "sourceAddresses": [
            "10.110.216.9"
          ],
          "programSelection": {
            "type": "PmtProgramNumber",
            "programNumber": "23"
          }
        }
      ],
      "streams": [],
      "id": "FNCHD"
    },
    {
      "type": "mpeg2tsUdp",
      "sources": [
        {
          "address": "239.194.208.216",
          "port": 2000,
          "sourceAddresses": [
            "10.110.208.216"
          ],
          "programSelection": {
            "type": "PmtProgramNumber",
            "programNumber": "702"
          }
        },
        {
          "address": "239.194.216.216",
          "port": 2000,
          "sourceAddresses": [
            "10.110.216.216"
          ],
          "programSelection": {
            "type": "PmtProgramNumber",
            "programNumber": "702"
          }
        }
      ],
      "streams": [],
      "id": "MTV"
    },
    {
      "type": "mpeg2tsUdp",
      "sources": [
        {
          "address": "239.194.208.6",
          "port": 2000,
          "sourceAddresses": [
            "10.110.208.6"
          ],
          "programSelection": {
            "type": "PmtProgramNumber",
            "programNumber": "1"
          }
        },
        {
          "address": "239.194.216.6",
          "port": 2000,
          "sourceAddresses": [
            "10.110.216.6"
          ],
          "programSelection": {
            "type": "PmtProgramNumber",
            "programNumber": "1"
          }
        }
      ],
      "streams": [],
      "id": "AEHD"
    },
    {
      "type": "mpeg2tsUdp",
      "sources": [
        {
          "address": "239.1.1.1",
          "port": 2000,
          "sourceAddresses": [
            "10.100.1.1"
          ],
          "programSelection": {
            "type": "PmtProgramNumber",
            "programNumber": "3"
          }
        },
        {
          "address": "239.1.2.1",
          "port": 2000,
          "sourceAddresses": [
            "10.100.2.1"
          ],
          "programSelection": {
            "type": "PmtProgramNumber",
            "programNumber": "3"
          }
        }
      ],
      "streams": [],
      "id": "SLATE"
    }
  ],
  "id": "Inputs",
  "name": "Inputs"
}```



Anything I tried; including removing the quotes around \n and ",", either failed the script completely or changed nothing.
68de4m5k

68de4m5k1#

下面是使用tonumber一次的压缩版本:

jq -Rn '{
  services: [
    inputs | select(length > 0) / "," | {
      type: "mpeg2tsUdp",
      sources: [range(2) as $i | {
        address: .[1+$i],
        port: 2000,
        sourceAddresses: [.[3+$i]],
        programSelection: {
          type: "PmtProgramNumber",
          programNumber: .[5] | tonumber
        }
      }],
      streams: [],
      id: .[0]
    }
  ],
  id: "Inputs",
  name: "Inputs"
}' < SourceList.csv

相关问题