Skip to content

Instantly share code, notes, and snippets.

@AlexNolasco
Last active August 16, 2024 14:02
Show Gist options
  • Save AlexNolasco/f68994d98e37b0658caf1fdef4fbb1aa to your computer and use it in GitHub Desktop.
Save AlexNolasco/f68994d98e37b0658caf1fdef4fbb1aa to your computer and use it in GitHub Desktop.
quick and dirty json convert int and nulls to strings
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
@AlexNolasco
Copy link
Author

Find pharmacyClasses

jq '.[].pharmacyClass' 2nd2.json | sort | uniq

@AlexNolasco
Copy link
Author

AlexNolasco commented Aug 13, 2024

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"


@AlexNolasco
Copy link
Author

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"


@AlexNolasco
Copy link
Author

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"

@AlexNolasco
Copy link
Author

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