-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding script to cleanup sample output from json consumer
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
source ./scripts/util.sh | ||
|
||
if [ "$#" -ne 1 ]; then | ||
echo "Usage: $0 <input_file>" | ||
exit 1 | ||
fi | ||
|
||
input_file="$1" | ||
|
||
if [ ! -f "$input_file" ]; then | ||
echo "Error: File '$input_file' not found!" | ||
exit 1 | ||
fi | ||
|
||
output_file="${input_file%.txt}.json" | ||
clean_output_file="${output_file%.json}_clean.json" | ||
|
||
# Remove escape characters before quotes in JSON property names | ||
# Using 'sed' to replace '\"' with '"' | ||
sed -i 's/\\"/"/g' "$input_file" | ||
|
||
# Run the jq command to create an array of JSON objects. | ||
# The input file usually looks like: | ||
# {...} | ||
# {...} | ||
# And we want this to look like: | ||
# [{...},{...}] | ||
# To be valid JSON. | ||
jq -s '.' "$input_file" > "$output_file" | ||
|
||
# Now we need to transform a msg string to json object for even better results. | ||
# Recursively clean the "msg" property in each object using jq. | ||
# Attempts to parse the msg content as JSON (fromjson?). If parsing fails, the original content is kept (// .). | ||
jq 'walk(if type == "object" and has("msg") then .msg |= (fromjson? // .) else . end)' "$output_file" > "$clean_output_file" | ||
|
||
# Removed unused $output_file | ||
rm -rf $output_file | ||
|
||
if [ $? -eq 0 ]; then | ||
echo "Successfully cleaned up '$input_file' to '$clean_output_file'" | ||
else | ||
echo "Error: Failed to process input file" | ||
exit 1 | ||
fi |