Skip to content

Instantly share code, notes, and snippets.

@roeniss
Last active September 14, 2023 18:33
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 roeniss/9f1c2c1220ceb926f19acd36a1e1ca30 to your computer and use it in GitHub Desktop.
Save roeniss/9f1c2c1220ceb926f19acd36a1e1ca30 to your computer and use it in GitHub Desktop.
convert kotlin data class toString into json in terminal
# motivation: it's not easy to look println(dataClass)
#
# example:
# input: Movie(title=Inception, year=2010, director=Person(firstName=Christopher, lastName=Nolan))
# output: {
# "title": "Inception",
# "year": "2010",
# "director": {
# "firstName": "Christopher",
# "lastName": "Nolan"
# }
# }
#
# note: I used gnu-sed (brew install gnu-sed) on macOS
echo "$DATA" \
| sed -E 's/\w+\(/\(/g' \
| sed -E 's/=/"="/g' \
| sed -E 's/, /", "/g' \
| sed -E 's/\(/\{"/g' \
| sed -E 's/(\w)\)/\1"\)/g' \
| sed -E 's/\)/\}/g' \
| sed -E 's/="\{/:\{/g' \
| sed -E 's/\}"/\}/g' \
| sed -E 's/=/:/g' \
| jq
@roeniss
Copy link
Author

roeniss commented Sep 10, 2023

example with Alfred5:

roeniss 2023-09-11 at 01 15 38

@roeniss
Copy link
Author

roeniss commented Sep 14, 2023

NOTE: this snippet has many caveats, so I don't use it anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment