Skip to content

Instantly share code, notes, and snippets.

@ajmaradiaga
Last active September 29, 2023 07:41
Show Gist options
  • Save ajmaradiaga/6d21f57f8a4e19f2073194ca18277de3 to your computer and use it in GitHub Desktop.
Save ajmaradiaga/6d21f57f8a4e19f2073194ca18277de3 to your computer and use it in GitHub Desktop.
Process Postman export files
#!/bin/bash
##############
# The zip file provided by Postman of a full export will include two folders, environment and collections.
# Within these folder there will be many files with guids as filenames. Making it non-user friendly
# to know the file of a specific environment/collection.
#
# The simple script below will rename the files by extracting the name within the contents of the file.
##############
# Check if jq is installed
if ! command -v jq &>/dev/null; then
echo "jq is not installed. Please install it before running this script."
exit 1
fi
# Specify the folder where your JSON files are located
json_folder="$1"
output_folder="${1}/insomnia"
mkdir -p "$output_folder"
# Iterate through each JSON file in the folder
for json_file in "$json_folder"/*.json; do
# Check if the file is a regular file
if [ -f "$json_file" ]; then
# Set default path to search for a name in the JSON file
# In an environment the name is in the .name JSON path
name_path=".name"
file_type="environment"
if jq -e '.info.name' "$json_file" &>/dev/null; then
# The file is a collection, path is .info.name
name_path=".info.name"
file_type="collection"
fi
# Use jq to extract the name from the file
name=$(jq -r "$name_path" "$json_file")
# Check if .info.name is not null or empty or its value is null
if [ -n "$name" ] && [ "$name" != "null" ]; then
# Rename the JSON file with the extracted name
new_name="${output_folder}/${name}.json"
echo '$file_type'
if [ "$file_type" = "environment" ]; then
# Convert Postman environment to Insomnia environment (key value pair)
jq -s 'map(.values[] | select(.enabled == true) | {(.key): .value}) | add' "$json_file" > "$new_name"
echo "Processed environment '$json_file' to '$new_name'"
else
cp "$json_file" "$new_name"
echo "Renamed '$json_file' to '$new_name'"
fi
else
echo "JSON file '$json_file' does not contain a name or it is empty."
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment