I have a bunch of separate .json files in one folder. Like this:
P50_00001.json
P50_00002.json
P50_00003.json
P50_00004.json
P50_00005.json....
Lots of them.
If you open them – they all look like this:
{
"Participant_id": "P50_00001",
"no_of_people": "Single",
"apparent_gender": "M",
"geographic_location": "JPN",
"ethnicity": "Japanese",
"capture_device_used": "iOS14",
"camera_orientation": "Portrait",
"camera_position": "Frontal View",
"Indoors_Outdoors_env": "Outdoors",
"lighting_condition": "Medium",
"Occluded": "None",
"scene_category": "Nature",
"camera_movement": "Still",
"action": "No action",
"Indoors_Outdoors_in_moving_car_or_train": "Outdoors",
"daytime_nighttime": "daytime"
}
But I need them to have square brackets in the beginning and the end:
[
{
"Participant_id": "P50_00001",
"no_of_people": "Single",
"apparent_gender": "M",
"geographic_location": "JPN",
"ethnicity": "Japanese",
"capture_device_used": "iOS14",
"camera_orientation": "Portrait",
"camera_position": "Frontal View",
"Indoors_Outdoors_env": "Outdoors",
"lighting_condition": "Medium",
"Occluded": "None",
"scene_category": "Nature",
"camera_movement": "Still",
"action": "No action",
"Indoors_Outdoors_in_moving_car_or_train": "Outdoors",
"daytime_nighttime": "daytime"
}
]
How can I batch-add square brackets to all the .json files in one directory at once?
Any way possible – powershell, javascript, python or even some sort of notepad, that’s why I tag all of it.
Thanks!
2
Answers
You can do this by using PowerShell:
Note the comma before the
@($value)
. This is needed to get PowerShell to add a superfluous layer of list. Otherwise, single-item lists are dissolved into their item immediately.A PowerShell solution:
For simplicity and speed, you can do plain-text processing:
Note: The
-WhatIf
common parameter in the command above previews the operation. Remove-WhatIf
once you’re sure the operation will do what you want.Note the use of
-Encoding utf8
to control the output encoding; adjust as needed (the input encoding is not guaranteed to be preserved so omitting-Encoding
will give youSet-Content
‘s default encoding, which is ANSI in Windows PowerShell, and BOM-less UTF-8 in PowerShell (Core)).