From 65efb506fa99dec35a510abf1e02769c20df4355 Mon Sep 17 00:00:00 2001 From: andream16 Date: Fri, 11 Oct 2024 17:34:09 +0100 Subject: [PATCH] Adding script to cleanup sample output from json consumer --- scripts/cleanup_stdout_json_consumer_out.sh | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 scripts/cleanup_stdout_json_consumer_out.sh diff --git a/scripts/cleanup_stdout_json_consumer_out.sh b/scripts/cleanup_stdout_json_consumer_out.sh new file mode 100755 index 000000000..0bf64e266 --- /dev/null +++ b/scripts/cleanup_stdout_json_consumer_out.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +set -e + +source ./scripts/util.sh + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + 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