Skip to main content
clean up
Source Link
steeldriver
  • 83.9k
  • 12
  • 124
  • 175

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.

Given

$ cat test
unix_idx
web_pn_iis
wis_healthpartners

and a JSON template

$ cat template.json
{
"name": "",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
}

then using jq

$ jq --rawfile names test '
    . | [foreach ($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.

Note that if the entries in the test file were properly quoted JSON strings, you could make it a bit neater:

$ cat test
"unix_idx"
"web_pn_iis"
"wis_healthpartners"

then

$ jq --slurpfile names test '. | [foreach $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"
  }
]

If the entries in the test file were properly quoted JSON strings

$ cat test
"unix_idx"
"web_pn_iis"
"wis_healthpartners"

then you could use jq's --slurpfile to pull them into an array, then iterate over that to create an array of objects from a template JSON file:

{
  "name": "",
  "datatype": "event",
  "searchableDays": 180,
  "maxDataSizeMB": 0,
  "totalEventCount": "0",
  "totalRawSizeMB": "0"
}

ex.

$ jq --slurpfile names test '
    . | [foreach $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"
  }
]

If you must work with unquoted elements, then you can do the same with --rawfile although it's a bit more work:

jq --rawfile names test '
    . | [foreach ($names | split("\n")[0:-1] | .[]) as $name (.; .name |= $name)]
' template.json

The slice [0:-1] is necessary because split("\n") interprets the test file's trailing newline as indicative of an additional empty element.

Source Link
steeldriver
  • 83.9k
  • 12
  • 124
  • 175

Given

$ cat test
unix_idx
web_pn_iis
wis_healthpartners

and a JSON template

$ cat template.json
{
"name": "",
"datatype": "event",
"searchableDays": 180,
"maxDataSizeMB": 0,
"totalEventCount": "0",
"totalRawSizeMB": "0"
}

then using jq

$ jq --rawfile names test '
    . | [foreach ($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.

Note that if the entries in the test file were properly quoted JSON strings, you could make it a bit neater:

$ cat test
"unix_idx"
"web_pn_iis"
"wis_healthpartners"

then

$ jq --slurpfile names test '. | [foreach $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"
  }
]