skip to Main Content

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


  1. You can do this by using PowerShell:

    $files = Get-ChildItem | Where Name -Like "*.json"
    foreach ($file in $files) {
        $value = Get-Content $file | ConvertFrom-Json
        $value = ,@($value)
        $value | ConvertTo-Json | Set-Content $file
    }
    

    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.

    Login or Signup to reply.
  2. A PowerShell solution:

    For simplicity and speed, you can do plain-text processing:

    # Defaults to the current dir; add -LiteralPath $yourDir as needed.
    Get-ChildItem -Filter *.json -PipelineVariable file |
      ForEach-Object {
        "[`n" + ($_ | Get-Content -Raw) + "]" # Enclose existing content in [ ... ]
      } | 
      Set-Content -Encoding utf8 -LiteralPath { $file.FullName } -WhatIf
    

    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 you Set-Content‘s default encoding, which is ANSI in Windows PowerShell, and BOM-less UTF-8 in PowerShell (Core)).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search