Last active
February 28, 2025 22:19
-
-
Save niltonvasques/4bf847d3ab272d3ba61febb4475e20da to your computer and use it in GitHub Desktop.
Convert JSON GraphQL output to GraphQL Input argument Syntax
This file contains hidden or 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
| #!/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