You don't dump a string but to a string.
d = {"foo": "bar"}
j = json.dumps(d)
print(j)
Output:
{"foo": "bar"}
It makes no sense to use a string as the argument of json.dumps which is expecting a dictionary or a list/tuple as the argument.
Answer from user1907906 on Stack OverflowSerialize
objto a JSON formatted str using this conversion table.
python - Using json.dumps(string) to dump a string - Stack Overflow
Python json dump - Stack Overflow
json.dump with python.dump
python json dumps - Stack Overflow
Videos
You don't dump a string but to a string.
d = {"foo": "bar"}
j = json.dumps(d)
print(j)
Output:
{"foo": "bar"}
It makes no sense to use a string as the argument of json.dumps which is expecting a dictionary or a list/tuple as the argument.
Serialize
objto a JSON formatted str using this conversion table.
In [17]: d = {"foo": "bar"}
In [18]: j = json.dumps(d)
In [19]: j
Out[19]: '{"foo": "bar"}'
Now if you need your dictionary back instead of a string
In [37]: l=json.loads(j)
In [38]: l
Out[38]: {u'foo': u'bar'}
or alternatively
In [20]: import ast
In [22]: k=ast.literal_eval(j)
In [23]: k
Out[23]: {'foo': 'bar'}
The last % is the first character of your console prompt line or a feature of your shell (https://unix.stackexchange.com/questions/167582/why-zsh-ends-a-line-with-a-highlighted-percent-symbol)
Nothing to do with json, nor python.
Because, when print() add a '\n' at the end, the dump to stdout doesn't
The reason is that print(json.dumps(data, indent=4)) prints a newline, and json.dump(data, sys.stdout, indent=4) does not.
You can try adding a print() at the end:
print(50*'-')
print(json.dumps(data, indent=4))
print(50*'-')
json.dump(data, sys.stdout, indent=4)
print()
Is the % symbol part of your shell's prompt?
When you do json.dump you can pass data into the function and where to put it, and it creates a new json file. How could I do this but have the language be python when it dumps? I need to do this because json does not read tuples and other things I am wanting to save into the file. Thanks
This works but doesn't seem too elegant
import json
json.dumps(json.JSONDecoder().decode(str_w_quotes))
json.dumps thinks that the " is part of a the string, not part of the json formatting.
import json
json.dumps(json.load(str_w_quotes))
should give you:
[{"id": 2, "name": "squats", "wrs": [["55", 9]]}]