Created
November 1, 2019 04:13
-
-
Save CliffordAnderson/aa9755000afae05f38da20731eaff89e to your computer and use it in GitHub Desktop.
CVS to JSONL example
This file contains 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
xquery version "3.1"; | |
declare namespace xpf = "http://www.w3.org/2005/xpath-functions"; | |
(: Converts a CSV into JSONL, i.e. one JSON object per line :) | |
let $csv := fetch:text("https://gist.githubusercontent.com/CliffordAnderson/588774dd0fa7d7bb616abd8da478fe76/raw/33395bdb6fce8d215464738f8681b66b130159e9/books.csv") => csv:parse(map { 'header': true() }) | |
for $record in $csv/csv/record | |
let $key-values := | |
for $entry in $record/* | |
return | |
if ($entry castable as xs:integer) | |
then <xpf:number key="{fn:name($entry)}">{$entry/text()}</xpf:number> | |
else <xpf:string key="{fn:name($entry)}">{$entry/text()}</xpf:string> | |
let $line := <xpf:map>{$key-values}</xpf:map> | |
return xml-to-json($line, map { 'indent': false() }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment