{"id":19096121,"url":"https://github.com/fsistemas/gosqltojson","last_synced_at":"2026-04-12T09:10:16.639Z","repository":{"id":74378799,"uuid":"538236384","full_name":"fsistemas/gosqltojson","owner":"fsistemas","description":"gosqltojson is a go version of sql2json(python3). Help to automate sql query execution and export to csv, json","archived":false,"fork":false,"pushed_at":"2022-10-09T03:25:12.000Z","size":41,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-09T03:38:33.584Z","etag":null,"topics":["automation","command-line-tool","csv","database","go","json","mysql","postgres","sql","tool"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fsistemas.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-18T20:44:31.000Z","updated_at":"2023-08-19T01:40:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"ee20a728-c672-45d3-9f7e-9935b9569f0a","html_url":"https://github.com/fsistemas/gosqltojson","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsistemas%2Fgosqltojson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsistemas%2Fgosqltojson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsistemas%2Fgosqltojson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsistemas%2Fgosqltojson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fsistemas","download_url":"https://codeload.github.com/fsistemas/gosqltojson/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232271972,"owners_count":18497773,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","command-line-tool","csv","database","go","json","mysql","postgres","sql","tool"],"created_at":"2024-11-09T03:35:53.788Z","updated_at":"2026-04-12T09:10:16.605Z","avatar_url":"https://github.com/fsistemas.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gosqltojson\n\n## What is gosqltojson\ngosqltojson is a command line tool to automate execution of sql queries and show the result in your screen or save it to a file(json or csv). \nOutput formats: json, csv.\n\n## Limitations\n- CSV works only with output file flag --output\n\ngosqltojson is a go implementation of [sql2json](https://github.com/fsistemas/sql2json), sql2json was written in python3.\n\n## Why create gosqltojson if sql2json(python version) does well the job?\n- Short version: For fun\n- Long version: To practice and to improve my skills with go programming language creating a useful and real project\n\n# How to use gosqltojson\n\n## Available options and help\ngosqltojson -help\n\n## Default output format\nThe default output format is json.\n\n## Configuration file\ngosqltojson by default use a config file located at USER_HOME/.gosqltojson/config.json\n\nconfig.json structure:\n\n```\n{\n    \"connections\": {\n        \"default\": \"sqlite+test.db\",\n        \"postgress\": \"postgres+scott:tiger@localhost:5432/mydatabase\",\n        \"mysql\": \"mysql+scott:tiger@localhost/foo\"\n    },\n    \"queries\": {\n        \"default\": \"SELECT 1 AS a, 2 AS b\",\n        \"sales_month_since\": \"SELECT inv.month, SUM(inv.amount) AS sales FROM invoices inv WHERE inv.date \u003e= @date_from \",\n        \"total_sales_since\": \"SELECT SUM(inv.amount) AS sales FROM invoices inv WHERE inv.date \u003e= @date_from \",\n        \"long_query\": \"@FULL_PATH_TO_SQL_FILE\",\n\t\t\"json\": \"SELECT JSON_OBJECT('id', 87, 'name', 'carrot') AS json\",\n\t\t\"jsonarray\": \"SELECT JSON_ARRAY(1, 'abc', NULL, TRUE) AS jsonarray, JSON_OBJECT('id', 87, 'name', 'carrot') AS jsonobject\",\n        \"operation_parameters\": \"@/Users/myusername/myproject/my-super-test_query.sql\"\n    }\n}\n```\n\n## Use a config.json in a different path\n\nYou can use gosqltojson --config PATH_TO_YOUR_CONFIG_FILE\n\n## Available variables to do your life easy:\n- START_CURRENT_MONTH: Date the first day of current month\n- CURRENT_DATE: Current Date\n- END_CURRENT_MONTH: Date the last day of current month\n- START_CURRENT_YEAR: First day of current year\n- END_CURRENT_YEAR: First day of current year\n\n## Date formats to CURRENT_DATE, START_CURRENT_MONTH, END_CURRENT_MONTH, START_CURRENT_YEAR, END_CURRENT_YEAR\nYou can use date format supported by go, default is 2006-01-02(YYYY-MM-DD)\n\nFor example:\n\n```\ngosqltojson -query \"SELECT @firstDayLastMonth AS dateFrom, @lastDayThisYear AS dateTo\" - --firstDayLastMonth \"START_CURRENT_MONTH-1\" --lastDayThisYear \"END_CURRENT_YEAR\"\n```\n\nQuery result:\n```\n[\n    {\n        \"dateFrom\": \"2022-08-01\",\n        \"dateTo\": \"2022-12-31\"\n    }\n]\n```\n\n## How to run queries using gosqltojson:\n\n### Run query sales_month in database connection mysql:\n\ngosqltojson --name mysql --query sales_month_since --date_from \"START_CURRENT_MONTH-1\"\n\nOutput:\n\n```\n[\n    {\n        \"month\": \"January\",\n        \"sales\": 5000\n    },\n    {\n        \"month\": \"February\",\n        \"sales\": 3000\n    }\n]\n```\n\n### I don't wat an array, I want an object with an attribute with the results, useful to generate in format to post to geckoboard\n\ngosqltojson -name mysql -query sales_month_since -wrapper data - --date_from \"START_CURRENT_MONTH-1\"\n\nOutput:\n\n```\n{\n    \"data\": [\n        {\n            \"month\": \"January\",\n            \"sales\": 5000\n        },\n        {\n            \"month\": \"February\",\n            \"sales\": 3000\n        }\n    ]\n}\n```\n\n### Run query sales_month in database connection mysql, use month as key, sales as value:\n\ngosqltojson -name mysql -query sales_month_since -key month -value sales - --date_from \"START_CURRENT_MONTH-1\"\n\nOutput:\n\n```\n[\n    {\n        \"January\": 5000\n    },\n    {\n        \"sales\": 3000\n    }\n]\n```\n\n### Run query sales_month in database connection mysql, get the first row and only sales amount:\n\ngosqltojson -name mysql -query total_sales_since -first -key sales - --date_from \"CURRENT_DATE-10\"\n\nOutput: 500 or the amount of money you have sold last 10 days\n\n### When I use gosqltojson with result of JSON functions I get escaped strings as value\n\ngosqltojson as a flag to allow you to specify your JSON columns\n\ngosqltojson -name mysql -query json -jsonkeys \"json, jsonarray\"\n\nResult:\n\n```\n[\n    {\n        \"json\": {\n            \"id\":  87,\n            \"name\", \"carrot\"\n        }\n        \"jsonarray\": [1, \"abc\", null, true],\n    }\n]\n```\n\n## This is only a single row I want first row only, no array.\n\ngosqltojson -name mysql -query json -jsonkeys \"json, jsonarray\" -first\n\nResult:\n\n```\n    {\n        \"json\": {\n            \"id\":  87,\n            \"name\", \"carrot\"\n        }\n        \"jsonarray\": [1, \"abc\", null, true],\n    }\n```\n\n### I have a long query. How to run sql query in external sql file?\n\nquery \"operation_parameters\"\nPath \"Users/myusername/myproject/my-super-query.sql\"\n\nContent of my-super-query.sql:\n\n```\nSELECT \np.name,\np.age\nFROM persons p\nWHERE p.age \u003e :min_age\nAND p.creation_date \u003e :min_date\nORDER BY p.age DESC\nLIMIT 10\n```\n\nmin_age: 18\nmin_date: Today YYYY-MM-DD\n\ngosqltojson -name mysql -query operation_parameters - --min_age 18 --min_date \"CURRENT_DATE\"\n\n```\n[\n    {\n        \"age\": \"40\",\n        \"name\": \"p4\"\n    },\n    {\n        \"age\": \"30\",\n        \"name\": \"p3\"\n    },\n    {\n        \"age\": \"20\",\n        \"name\": \"p2\"\n    }\n]\n```\n\nmin_age: 18\nmin_date: First day, current year YYYY-01-01 00:00:00\n\ngosqltojson -name mysql -query operation_parameters - --min_age 10 --min_date START_CURRENT_YEAR\n\n```\n[\n    {\n        \"age\": \"40\",\n        \"name\": \"p4\"\n    },\n    {\n        \"age\": \"30\",\n        \"name\": \"p3\"\n    },\n    {\n        \"age\": \"20\",\n        \"name\": \"p2\"\n    },\n    {\n        \"age\": \"12\",\n        \"name\": \"p1\"\n    }\n]\n```\n\n### How to run external SQL query not defined in config file?\n\ngosqltojson -name mysql -query \"@/Users/myusername/myproject/my-super-query.sql\" - --min_age 18 --min_date START_CURRENT_YEAR\n\n```\n[\n    {\n        \"age\": \"40\",\n        \"name\": \"p4\"\n    },\n    {\n        \"age\": \"30\",\n        \"name\": \"p3\"\n    },\n    {\n        \"age\": \"20\",\n        \"name\": \"p2\"\n    }\n]\n```\n\n### Run custom query inline\n\nYou don't need to have all your queries in config file\n\ngosqltojson -name mysql -query \"SELECT CURRENT_DATE() AS date\" -first -key date\n\n```\n2022-09-25\n```\n\n\n### Write sql query result to a CSV file\n\ngosqltojson -name mysql -query sales_month_since -format=csv -output Sales - --date_from \"START_CURRENT_MONTH-1\"\n\n```\nOutput:\nSales.csv\n```\n\n### Write sql query result to an Excel file\n\nThat feature is not available yet, maybe in the future.\nIf you really need excel please try [sql2json](https://github.com/fsistemas/sql2json)\n\n### Write sql query result to a json file\n\ngosqltojson -name mysql -query sales_month_since -format=json -output Sales - --date_from \"START_CURRENT_MONTH-1\"\n\nOutput:\n```\nSales.json\n```\n\n# How to run during development\n\ngo run . -config config.json -query @test_query.sql -wrapper data -- -x 1 -y 2 -z 3 -a 12\n\ngo run . -config config.json -name golang -query @mysqlquery.sql -wrapper data  -- -a 11\n\ngo run . -config config.json -name golang -query @mysqlquery.sql -wrapper data  -- -date_from CURRENT_DATE\n\ngo run . -config config.json -name golang -query @mysqlquery.sql -wrapper data  -- -date_from START_CURRENT_MONT-1\n\n./gosqltojson -query @test_query.sql -format=json -output output.json - --a 2\n./gosqltojson -query @test_query.sql -format=csv -output output.csv - --a 2\n\n## How to build\ngo build\ngo build -ldflags=\"-s -w\"\n\n## Run Unit Test\ngo test ./...\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffsistemas%2Fgosqltojson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffsistemas%2Fgosqltojson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffsistemas%2Fgosqltojson/lists"}