Skip to content

Instantly share code, notes, and snippets.

@niltonvasques
Last active February 28, 2025 22:19
Show Gist options
  • Select an option

  • Save niltonvasques/4bf847d3ab272d3ba61febb4475e20da to your computer and use it in GitHub Desktop.

Select an option

Save niltonvasques/4bf847d3ab272d3ba61febb4475e20da to your computer and use it in GitHub Desktop.
Convert JSON GraphQL output to GraphQL Input argument Syntax
#!/bin/bash
# Converts JSON to GraphQL Input Syntax
json2graphql() {
jq -r '
def convert:
if type == "object" then
"{ " + (to_entries | map(.key + ": " + (.value | convert)) | join(" ")) + " }"
elif type == "array" then
"[ " + (map(convert) | join(" ")) + " ]"
elif type == "string" then
@json
else
tostring
end;
convert
'
}
# Read from stdin and process JSON
cat | json2graphql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment