Insert JSON String in to PostgreSQL table column
Trying to insert following JSON String as is without formatting
str=
"Trial":
"@id": "xxxxx",
"key1": "aaaaaaaa (BAL-8557) aaaaaaaaaaaaaaaaaa",
"key2": "aaaaaaaaaaaaa , aaaaaaaaaaaaaaaaaaa",
"key3": "Yes",
"key4": "No",
"key5":
"key5": [
"@type": "ABC",
"$": "ABC121 "
,
"@type": "ABC",
"$": "ABC12133 "
]
,
"Indications":
"Indication":
"@id": "1308",
"$": "aaaaaa"
in to following PostgreSQL DB
CREATE TABLE records
(
id text,
record json
)
Code I am using to perform the insert operation
cur = conn.cursor()
cur.execute("INSERT INTO trial_records(id, record) VALUES (%s, %s)", ('1', json.dumps(json.loads(str))))
Error I am getting
the JSON object must be str, bytes or bytearray, not 'dict'
Is this the right syntax to store json string as part of the column
python json postgresql
add a comment |
Trying to insert following JSON String as is without formatting
str=
"Trial":
"@id": "xxxxx",
"key1": "aaaaaaaa (BAL-8557) aaaaaaaaaaaaaaaaaa",
"key2": "aaaaaaaaaaaaa , aaaaaaaaaaaaaaaaaaa",
"key3": "Yes",
"key4": "No",
"key5":
"key5": [
"@type": "ABC",
"$": "ABC121 "
,
"@type": "ABC",
"$": "ABC12133 "
]
,
"Indications":
"Indication":
"@id": "1308",
"$": "aaaaaa"
in to following PostgreSQL DB
CREATE TABLE records
(
id text,
record json
)
Code I am using to perform the insert operation
cur = conn.cursor()
cur.execute("INSERT INTO trial_records(id, record) VALUES (%s, %s)", ('1', json.dumps(json.loads(str))))
Error I am getting
the JSON object must be str, bytes or bytearray, not 'dict'
Is this the right syntax to store json string as part of the column
python json postgresql
2
You should remove yourjson.loads
call. So it will bejson.dumps(str)
.
– sashaaero
Nov 12 '18 at 21:19
Thanks that solved it
– K.Pil
Nov 12 '18 at 22:52
@sashaaero put it as your answer.. So this question will not show in unanswered..
– dwir182
Nov 13 '18 at 0:03
add a comment |
Trying to insert following JSON String as is without formatting
str=
"Trial":
"@id": "xxxxx",
"key1": "aaaaaaaa (BAL-8557) aaaaaaaaaaaaaaaaaa",
"key2": "aaaaaaaaaaaaa , aaaaaaaaaaaaaaaaaaa",
"key3": "Yes",
"key4": "No",
"key5":
"key5": [
"@type": "ABC",
"$": "ABC121 "
,
"@type": "ABC",
"$": "ABC12133 "
]
,
"Indications":
"Indication":
"@id": "1308",
"$": "aaaaaa"
in to following PostgreSQL DB
CREATE TABLE records
(
id text,
record json
)
Code I am using to perform the insert operation
cur = conn.cursor()
cur.execute("INSERT INTO trial_records(id, record) VALUES (%s, %s)", ('1', json.dumps(json.loads(str))))
Error I am getting
the JSON object must be str, bytes or bytearray, not 'dict'
Is this the right syntax to store json string as part of the column
python json postgresql
Trying to insert following JSON String as is without formatting
str=
"Trial":
"@id": "xxxxx",
"key1": "aaaaaaaa (BAL-8557) aaaaaaaaaaaaaaaaaa",
"key2": "aaaaaaaaaaaaa , aaaaaaaaaaaaaaaaaaa",
"key3": "Yes",
"key4": "No",
"key5":
"key5": [
"@type": "ABC",
"$": "ABC121 "
,
"@type": "ABC",
"$": "ABC12133 "
]
,
"Indications":
"Indication":
"@id": "1308",
"$": "aaaaaa"
in to following PostgreSQL DB
CREATE TABLE records
(
id text,
record json
)
Code I am using to perform the insert operation
cur = conn.cursor()
cur.execute("INSERT INTO trial_records(id, record) VALUES (%s, %s)", ('1', json.dumps(json.loads(str))))
Error I am getting
the JSON object must be str, bytes or bytearray, not 'dict'
Is this the right syntax to store json string as part of the column
python json postgresql
python json postgresql
asked Nov 12 '18 at 21:14
K.PilK.Pil
10219
10219
2
You should remove yourjson.loads
call. So it will bejson.dumps(str)
.
– sashaaero
Nov 12 '18 at 21:19
Thanks that solved it
– K.Pil
Nov 12 '18 at 22:52
@sashaaero put it as your answer.. So this question will not show in unanswered..
– dwir182
Nov 13 '18 at 0:03
add a comment |
2
You should remove yourjson.loads
call. So it will bejson.dumps(str)
.
– sashaaero
Nov 12 '18 at 21:19
Thanks that solved it
– K.Pil
Nov 12 '18 at 22:52
@sashaaero put it as your answer.. So this question will not show in unanswered..
– dwir182
Nov 13 '18 at 0:03
2
2
You should remove your
json.loads
call. So it will be json.dumps(str)
.– sashaaero
Nov 12 '18 at 21:19
You should remove your
json.loads
call. So it will be json.dumps(str)
.– sashaaero
Nov 12 '18 at 21:19
Thanks that solved it
– K.Pil
Nov 12 '18 at 22:52
Thanks that solved it
– K.Pil
Nov 12 '18 at 22:52
@sashaaero put it as your answer.. So this question will not show in unanswered..
– dwir182
Nov 13 '18 at 0:03
@sashaaero put it as your answer.. So this question will not show in unanswered..
– dwir182
Nov 13 '18 at 0:03
add a comment |
1 Answer
1
active
oldest
votes
json.loads(obj)
creates json object from string data.
json.dumps(obj)
creates string from json object.
So you should change json.dumps(json.loads(str))
to json.dumps(str)
.
P.S.: I'd also don't recommend to call your variables as type names like str
, int
. You are making str()
and int()
call unable from current scope.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270215%2finsert-json-string-in-to-postgresql-table-column%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
json.loads(obj)
creates json object from string data.
json.dumps(obj)
creates string from json object.
So you should change json.dumps(json.loads(str))
to json.dumps(str)
.
P.S.: I'd also don't recommend to call your variables as type names like str
, int
. You are making str()
and int()
call unable from current scope.
add a comment |
json.loads(obj)
creates json object from string data.
json.dumps(obj)
creates string from json object.
So you should change json.dumps(json.loads(str))
to json.dumps(str)
.
P.S.: I'd also don't recommend to call your variables as type names like str
, int
. You are making str()
and int()
call unable from current scope.
add a comment |
json.loads(obj)
creates json object from string data.
json.dumps(obj)
creates string from json object.
So you should change json.dumps(json.loads(str))
to json.dumps(str)
.
P.S.: I'd also don't recommend to call your variables as type names like str
, int
. You are making str()
and int()
call unable from current scope.
json.loads(obj)
creates json object from string data.
json.dumps(obj)
creates string from json object.
So you should change json.dumps(json.loads(str))
to json.dumps(str)
.
P.S.: I'd also don't recommend to call your variables as type names like str
, int
. You are making str()
and int()
call unable from current scope.
answered Nov 13 '18 at 0:35
sashaaerosashaaero
8531720
8531720
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270215%2finsert-json-string-in-to-postgresql-table-column%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You should remove your
json.loads
call. So it will bejson.dumps(str)
.– sashaaero
Nov 12 '18 at 21:19
Thanks that solved it
– K.Pil
Nov 12 '18 at 22:52
@sashaaero put it as your answer.. So this question will not show in unanswered..
– dwir182
Nov 13 '18 at 0:03