Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can json.dumps create multiple lines

4,052 views
Skip to first unread message

Cecil Westerhof

unread,
Dec 1, 2016, 11:44:36 AM12/1/16
to
I started to use json.dumps to put things in a SQLite database. But I
think it would be handy when it would be easy to change the values
manually.

When I have a value dummy which contains:
['An array', 'with several strings', 'as a demo']
Then json.dumps(dummy) would generate:
'["An array", "with several strings", "as a demo"]'
I would prefer when it would generate:
'[
"An array",
"with several strings",
"as a demo"
]'

Is this possible, or do I have to code this myself?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Zachary Ware

unread,
Dec 1, 2016, 11:55:49 AM12/1/16
to
On Thu, Dec 1, 2016 at 10:30 AM, Cecil Westerhof <Ce...@decebal.nl> wrote:
> I would prefer when it would generate:
> '[
> "An array",
> "with several strings",
> "as a demo"
> ]'
>
> Is this possible, or do I have to code this myself?

https://docs.python.org/3/library/json.html?highlight=indent#json.dump

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.dumps(["An array", "with several strings", "as a demo"])
'["An array", "with several strings", "as a demo"]'
>>> print(_)
["An array", "with several strings", "as a demo"]
>>> json.dumps(["An array", "with several strings", "as a demo"], indent=0)
'[\n"An array",\n"with several strings",\n"as a demo"\n]'
>>> print(_)
[
"An array",
"with several strings",
"as a demo"
]

I've also seen something about JSON support in SQLite, you may want to
look into that.

--
Zach

John Gordon

unread,
Dec 1, 2016, 11:57:28 AM12/1/16
to
In <87lgvz4...@Equus.decebal.nl> Cecil Westerhof <Ce...@decebal.nl> writes:

> I started to use json.dumps to put things in a SQLite database. But I
> think it would be handy when it would be easy to change the values
> manually.

> When I have a value dummy which contains:
> ['An array', 'with several strings', 'as a demo']
> Then json.dumps(dummy) would generate:
> '["An array", "with several strings", "as a demo"]'
> I would prefer when it would generate:
> '[
> "An array",
> "with several strings",
> "as a demo"
> ]'

json.dumps() has an 'indent' keyword argument, but I believe it only
enables indenting of each whole element, not individual members of a list.

Perhaps something in the pprint module?

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Tim Chase

unread,
Dec 1, 2016, 3:27:59 PM12/1/16
to
On 2016-12-01 17:30, Cecil Westerhof wrote:
> When I have a value dummy which contains:
> ['An array', 'with several strings', 'as a demo']
> Then json.dumps(dummy) would generate:
> '["An array", "with several strings", "as a demo"]'
> I would prefer when it would generate:
> '[
> "An array",
> "with several strings",
> "as a demo"
> ]'
>
> Is this possible, or do I have to code this myself?

print(json.dumps(['An array', 'with several strings', 'as a demo'],
indent=0))

for the basics of what you ask, though you can change indent= to
indent the contents for readability.

-tkc



Cecil Westerhof

unread,
Dec 1, 2016, 4:59:26 PM12/1/16
to
Works like a charm. Strings can contain newlines also, but then I do
not want a new line, but that works like a charm.

I used:
cursor.execute('INSERT INTO test (json) VALUES (?)' ,
[json.dumps(['An array',
'with several strings',
'as a demo',
'and\none\nwith\na\nnewlines'],
indent = 0)])

and that gave exactly what I wanted.

Now I need to convert the database. But that should not be a big
problem.


> I've also seen something about JSON support in SQLite, you may want
> to look into that.

I will do that, but later. I have what I need.

Cecil Westerhof

unread,
Dec 1, 2016, 5:28:21 PM12/1/16
to
On Thursday 1 Dec 2016 22:52 CET, Cecil Westerhof wrote:

> Now I need to convert the database. But that should not be a big
> problem.

I did the conversion with:
cursor.execute('SELECT tipID FROM tips')
ids = cursor.fetchall()
for id in ids:
id = id[0]
cursor.execute('SELECT tip from tips WHERE tipID = ?', [id])
old_value = cursor.fetchone()[0]
new_value = json.dumps(json.loads(old_value), indent = 0)
cursor.execute('UPDATE tips SET tip = ? WHERE tipID = ?', [new_value, id])

Peter Otten

unread,
Dec 1, 2016, 5:58:53 PM12/1/16
to
Cecil Westerhof wrote:

> On Thursday 1 Dec 2016 22:52 CET, Cecil Westerhof wrote:
>
>> Now I need to convert the database. But that should not be a big
>> problem.
>
> I did the conversion with:
> cursor.execute('SELECT tipID FROM tips')
> ids = cursor.fetchall()
> for id in ids:
> id = id[0]
> cursor.execute('SELECT tip from tips WHERE tipID = ?', [id])
> old_value = cursor.fetchone()[0]
> new_value = json.dumps(json.loads(old_value), indent = 0)
> cursor.execute('UPDATE tips SET tip = ? WHERE tipID = ?',
> [new_value, id])

The sqlite3 module lets you define custom functions written in Python:

db = sqlite3.connect(...)
cs = db.cursor()

def convert(s):
return json.dumps(
json.loads(s),
indent=0
)

db.create_function("convert", 1, convert)
cs.execute("update tips set tip = convert(tip)")


Cecil Westerhof

unread,
Dec 1, 2016, 6:28:23 PM12/1/16
to
That is a lot better as what I did. Thank you.

Thomas 'PointedEars' Lahn

unread,
Dec 1, 2016, 7:15:17 PM12/1/16
to
Cecil Westerhof wrote:

> I started to use json.dumps to put things in a SQLite database.

You SHOULD NOT do that.

Either use a relational database like SQLite, then use object-relational
mappers to store object data in a table-row-field relational, normalized
way.

Or use an object database like MongoDb, then you do not have to store string
representations of objects, but can store the objects themselves (and
manipulate them there with great comfort).

> But I think it would be handy when it would be easy to change the values
> manually.

That you are actually considering to *modify substrings* of database values
to modify data, and that you therefore worry about how those values are
formatted, should be a clear sign to you that your approach is wrong.

You have an X–Y problem.

--
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Joaquin Alzola

unread,
Dec 3, 2016, 12:38:03 AM12/3/16
to
On Thu, Dec 1, 2016 at 10:30 AM, Cecil Westerhof <Ce...@decebal.nl> wrote:
> I would prefer when it would generate:
> '[
> "An array",
> "with several strings",
> "as a demo"
> ]'
>
> Is this possible, or do I have to code this myself?

> https://docs.python.org/3/library/json.html?highlight=indent#json.dump

>>>> json.dumps(["An array", "with several strings", "as a demo"], indent=0)
>'[\n"An array",\n"with several strings",\n"as a demo"\n]'
>>>> print(_)
>[
>"An array",
>"with several strings",
>"as a demo"
>]

As Zac stated the indent:
>>> print(json.dumps(["An array",{"Dummy":{'wop':'dop','dap':'dap'}}, "with several strings", "as a demo"], sort_keys = True, indent=4))
[
"An array",
{
"Dummy": {
"dap": "dap",
"wop": "dop"
}
},
"with several strings",
"as a demo"
]
>>>
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.
0 new messages