Skip to content

Instantly share code, notes, and snippets.

@kyounger
Created June 7, 2019 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyounger/ae52dbee1614f39067ef0ec593d41818 to your computer and use it in GitHub Desktop.
Save kyounger/ae52dbee1614f39067ef0ec593d41818 to your computer and use it in GitHub Desktop.
Convert xml from ebscohost to csv using xml2json and jq
# tools required:
# npm install -g xml2json-cli
# brew install jq
# This command will flatten the resulting array of objects so that the headers are just the json path to the key joined with dots.
xml2json file.xml | jq '[.records.rec[] | . as $in | reduce leaf_paths as $path ({}; . + { ($path | map(tostring) | join(".")): $in | getpath($path) })]' | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' > test2.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment