Skip to content

Instantly share code, notes, and snippets.

@micheee

micheee/parse.xq Secret

Last active November 20, 2017 06:21
Show Gist options
  • Save micheee/8a8734a1713a7121cab15eb3dfb389d9 to your computer and use it in GitHub Desktop.
Save micheee/8a8734a1713a7121cab15eb3dfb389d9 to your computer and use it in GitHub Desktop.
parse-json.xq
let $items := (
fetch:text('https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json')
=> parse-json() (: Convert to XQuery item representation :)
=> array:for-each(function($map){ (: For each entry in that array, do :)
element item { (: Construct an XML element named item :)
$map => map:keys()
=> for-each(function($key){ (: For each key in the map, do: :)
element { $key } { (: Return an element named $key :)
$map($key) (: …and the value of $map($key) :)
}
})
}
})
=> array:flatten() (: Converts the array to a sequence :)
)
for $item in $items
return $item (: …insert node $item as last into … :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment