GivenIf the entries in the test file were properly quoted JSON strings
$ cat test
unix_idx"unix_idx"
web_pn_iis"web_pn_iis"
wis_healthpartners"wis_healthpartners"
andthen you could use jq's --slurpfile to pull them into an array, then iterate over that to create an array of objects from a JSON template JSON file:
$ cat template.json
{
"name": "",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
}
then using jqex.
$ jq --rawfileslurpfile names test '
. | [foreach ($names | split("\n")[0:-1] | .[])$names[] as $name (.; .name |= $name)]
']' template.json
[
{
"name": "unix_idx",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
},
{
"name": "web_pn_iis",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
},
{
"name": "wis_healthpartners",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
}
]
The slice [0:-1] is necessary because split("\n") interpretsIf you must work with unquoted elements, then you can do the same with test--rawfile file's trailing newline as indicative of an additional empty element.
Note that if the entries in the test file were properly quoted JSON strings, you could make italthough it's a bit neatermore work:
$ cat test
"unix_idx"
"web_pn_iis"
"wis_healthpartners"
then
$ jq --slurpfilerawfile names test '
. | [foreach $names[]($names | split("\n")[0:-1] | .[]) as $name (.; .name |= $name)]']
' template.json
[
{
"name": "unix_idx",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
},
{
"name": "web_pn_iis",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
},
{
"name": "wis_healthpartners",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
}
]
The slice [0:-1] is necessary because split("\n") interprets the test file's trailing newline as indicative of an additional empty element.