Last active
August 16, 2024 14:02
-
-
Save AlexNolasco/f68994d98e37b0658caf1fdef4fbb1aa to your computer and use it in GitHub Desktop.
quick and dirty json convert int and nulls to strings
This file contains hidden or 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
jq 'def walk(f): . as $in | if type == "object" then reduce keys[] as $key ({}; . + {($key): ($in[$key] | walk(f))}) elif type == "array" then map(walk(f)) else f end; walk(if . == null then "null" elif type == "number" then tostring else . end)' ./Untitled-10.json | |
or | |
cat input.json | jq 'def walk(f): . as $in | if type == "object" then reduce keys[] as $key ({}; . + {($key): ($in[$key] | walk(f))}) elif type == "array" then map(walk(f)) else f end; walk(if . == null then "null" elif type == "number" then tostring else . end)' | |
or | |
jq 'def walk(f): . as $in | |
| if type == "object" then | |
reduce keys[] as $key ({}; . + {($key): ($in[$key] | walk(f))}) | |
elif type == "array" then | |
map(walk(f)) | |
else | |
f | |
end; | |
walk(if . == null then "null" | |
elif type == "number" then tostring | |
else . | |
end)' input.json > output.json |
extract body script
#!/bin/bash
# Default output filename
output_file="script.ts"
# Check if input JSON file is provided
if [ -z "$1" ]; then
echo "Usage: $0 <input_json_file> [output_script_file]"
exit 1
fi
# Input JSON file
input_json="$1"
# Use provided output filename if available
if [ "$2" ]; then
output_file="$2"
fi
# Extract the "body" field from the JSON and replace "\n" with actual newlines
jq -r '.body' "$input_json" | sed 's/\\n/\n/g' > "$output_file"
echo "Body extracted from $input_json and saved to $output_file"
extract parameters
#!/bin/bash
# Default output filename
output_file="params.ts"
# Check if input JSON file is provided
if [ -z "$1" ]; then
echo "Usage: $0 <input_json_file> [output_script_file]"
exit 1
fi
# Input JSON file
input_json="$1"
# Use provided output filename if available
if [ "$2" ]; then
output_file="$2"
fi
# Extract the "body" field from the JSON and replace "\n" with actual newlines
jq -r '.parameterInfo' "$input_json" | sed 's/\\n/\n/g' > "$output_file"
echo "Parameter info extracted from $input_json and saved to $output_file"
unextract body
#!/bin/bash
# Default input script file
input_file="script.ts"
# Check if destination JSON file is provided
if [ -z "$1" ]; then
echo "Usage: $0 <destination_json_file> [input_script_file]"
exit 1
fi
# Destination JSON file
destination_json="$1"
# Use provided input script file if available
if [ "$2" ]; then
input_file="$2"
fi
# Read the content of the input file and replace newlines with \n
modified_body=$(sed ':a;N;$!ba;s/\n/\n/g' "$input_file")
# Replace the body in the destination JSON file with the modified body
#jq --arg body "$modified_body" '.body = $body' "$destination_json" > updated.json
jq --arg body "$modified_body" '.body = $body' "$destination_json" > tmp.json && mv tmp.json "$destination_json"
echo "Body replaced in $destination_json and saved to updated.json"
unextract parameters
#!/bin/bash
# Default input script file
input_file="params.ts"
# Check if destination JSON file is provided
if [ -z "$1" ]; then
echo "Usage: $0 <destination_json_file> [input_script_file]"
exit 1
fi
# Destination JSON file
destination_json="$1"
# Use provided input script file if available
if [ "$2" ]; then
input_file="$2"
fi
# Read the content of the input file and replace newlines with \n
modified_body=$(sed ':a;N;$!ba;s/\n/\n/g' "$input_file")
# Replace the body in the destination JSON file with the modified body
#jq --arg body "$modified_body" '.body = $body' "$destination_json" > updated.json
jq --arg body "$modified_body" '.parameterInfo = $body' "$destination_json" > tmp.json && mv tmp.json "$destination_json"
echo "Params replaced in $destination_json and saved to updated.json"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find pharmacyClasses
jq '.[].pharmacyClass' 2nd2.json | sort | uniq