Skip to content

Instantly share code, notes, and snippets.

@Lotti
Created April 29, 2021 14:36
Show Gist options
  • Save Lotti/96ea31bc5ca1d022b0ec626ed4f31666 to your computer and use it in GitHub Desktop.
Save Lotti/96ea31bc5ca1d022b0ec626ed4f31666 to your computer and use it in GitHub Desktop.
script to split array of objects in separated file. bash, input: json file, output: json files
FILE=discovery-insurance.json
FOLDER=docs-insurance
rm -Rf "./${FOLDER}"
ROWS=$(jq '.results | length' "./${FILE}")
if [[ "${ROWS}" -eq "0" ]]; then
echo "ARRAY WITH ZERO ELEMENTS. EXIT."
break
fi
mkdir -p "./${FOLDER}"
PATH_NAME="./${FOLDER}/"
for ((i = 0 ; i < ROWS ; i++ )); do
fullfile=$(jq --raw-output '.results['"${i}"'].id' "./${FILE}")
filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
jq '.results['"${i}"'] | del(.id, .result_metadata, .enriched_text, .extracted_metadata, .enriched_description, .enriched_operator_notes)' "./${FILE}" > "${PATH_NAME}${filename}.json"
# jq '.results['"${i}"'] | del(.id, .result_metadata)' "./${FILE}" > "${PATH_NAME}${filename}.json"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment