Repository: dynaconf/dynaconf
Pull request #813 (2022-09-23 16:23:17)

Ensure `dynaconf get` returns a valid json string.

netlify[bot]: ### <span aria-hidden="true">👷</span> Deploy Preview for *dynaconf* processing.


|  Name | Link |
|---------------------------------|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | e288e422ac8a07dd91af441f54ad70bdfafed50b |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/sites/dynaconf/deploys/632ddd7642738b0008ee8b8c |
github-actions[bot]: ## Test Results (Python 3.8)
368 tests  +1   368 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") +1   33s [:stopwatch:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "duration of all tests") ±0s
    1 suites ±0       0 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests") ±0 
    1 files   ±0       0 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") ±0 

Results for commit e288e422. ± Comparison against base commit 6395e97e.

[test-results]:data:application/gzip;base64,H4sIAKbeLWMC/13MSQ7CMBBE0atEXrPwFGK4DOq43ZJFEiMPK8TdcQJkYPl+SfVk5AeX2LURp4al4vMKLBGyD1OlUtV1yfOmzuanWyrW/qe7f9TE10Dgh0NwMYb4LbFM2+eM4+WnbI+Ld4eL9382jKPPFcxJY5yWEqwB3iFeBJDWgloN2PEeCchhy3v2egPbTEM0BAEAAA==

github-actions[bot]: ## Test Results (Python 3.10)
368 tests  +1   368 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") +1   34s [:stopwatch:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "duration of all tests") -10s
    1 suites ±0       0 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests") ±0 
    1 files   ±0       0 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") ±0 

Results for commit e288e422. ± Comparison against base commit 6395e97e.

[test-results]:data:application/gzip;base64,H4sIALDeLWMC/13Myw7CIBCF4VdpWLsASi36MmbKMMnEthgKK+O7S6v24vL7T3Kegrj3k7hW6lSJKXNagTlC4jAW1qa4LGne6rP96TZl5/7TnR8lyTUQcH8IPsYQvyXmcfuccbz8lO1x8e5w8f7PhWHgVCC8ttYbrcFZkC3iRQEZo6gxgK3skIA8NrITrzdq+2qIBAEAAA==


Patch:
diff --git a/dynaconf/cli.py b/dynaconf/cli.py
index 182bd0f..1490f37 100644
--- a/dynaconf/cli.py
+++ b/dynaconf/cli.py
@@ -1,6 +1,7 @@
 from __future__ import annotations
 
 import importlib
+import json
 import os
 import pprint
 import sys
@@ -439,7 +440,10 @@ def init(ctx, fileformat, path, env, _vars, _secrets, wg, y, django):
     is_flag=True,
 )
 def get(key, default, env, unparse):
-    """Returns the raw value for a settings key"""
+    """Returns the raw value for a settings key.
+
+    If result is a dict, list or tuple it is printes as a valid json string.
+    """
     if env:
         env = env.strip()
     if key:
@@ -456,6 +460,9 @@ def get(key, default, env, unparse):
     if unparse:
         result = unparse_conf_data(result)
 
+    if isinstance(result, (dict, list, tuple)):
+        result = json.dumps(result, sort_keys=True)
+
     click.echo(result, nl=False)
 
 


Test patch:
diff --git a/tests/config.py b/tests/config.py
index fa61a1c..33d1291 100644
--- a/tests/config.py
+++ b/tests/config.py
@@ -3,4 +3,8 @@ from __future__ import annotations
 from dynaconf import Dynaconf
 
 settingsenv = Dynaconf(environments=True)
-settings = Dynaconf(TEST_KEY="test_value", ANOTHER_KEY="another_value")
+settings = Dynaconf(
+    TEST_KEY="test_value",
+    ANOTHER_KEY="another_value",
+    DATA={"KEY": "value", "OTHERKEY": "other value"},
+)
diff --git a/tests/test_cli.py b/tests/test_cli.py
index ed6c948..3ff506d 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -125,6 +125,18 @@ def test_get(testdir):
     assert result == "test_value"
 
 
+def test_get_json_dict(testdir):
+    """Tests get command printing json"""
+    env = env = {
+        "ROOT_PATH_FOR_DYNACONF": testdir,
+        "DYNACONF_DATA__KEY": "value",
+        "DYNACONF_DATA__OTHERKEY": "other value",
+        "INSTANCE_FOR_DYNACONF": "tests.config.settings",
+    }
+    result = run(["get", "data"], env=env)
+    assert result == '{"KEY": "value", "OTHERKEY": "other value"}'
+
+
 def test_get_lower(testdir):
     """Tests get command"""
     result = run(