How to get desired JSON output given a Pymongo cursor with Marshmallow Schema
I am having a problem with my Marshmallow output, whereby the JSON result is:
[
[
{
"data":[
{
"x":"2018-04-03 23:28:31.539895",
"y":15.937
}
],
"sensor":"/sys/bus/w1/devices/28-0000077aae57/w1_slave"
},
],
{
}
]
But I want it to look like:
[
{
"data":[
{
"x":"2018-04-03 23:37:40.612334",
"y":16.187
}
],
"sensor":"/sys/bus/w1/devices/28-0000077aae57/w1_slave"
}
]
I have a get_line function which returns a Pymongo cursor:
>>> get_line()
<pymongo.cursor.Cursor object at 0x109162208>
I then define my marshmallow schemas:
from marshmallow import Schema, fields
class PointSchema(Schema):
x = fields.String()
y = fields.String()
class LineSchema(Schema):
sensor = fields.String()
data = fields.List(fields.Nested(PointSchema))
I use the following code within my flask endpoint to return the data to the client:
result = get_line()
output =
for line in result:
output.append({'sensor':line['name'], 'data': get_line(line['name'])})
return jsonify(LineSchema(many=True).dump(output))
I have also tried replacing LineSchema with:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
which results in the exact same JSON output. How should I be processing this to achieve the desired JSON? I'm ripping my hair out with this, but want to avoid changing the client's code to accommodate.
python pymongo marshmallow
add a comment |
I am having a problem with my Marshmallow output, whereby the JSON result is:
[
[
{
"data":[
{
"x":"2018-04-03 23:28:31.539895",
"y":15.937
}
],
"sensor":"/sys/bus/w1/devices/28-0000077aae57/w1_slave"
},
],
{
}
]
But I want it to look like:
[
{
"data":[
{
"x":"2018-04-03 23:37:40.612334",
"y":16.187
}
],
"sensor":"/sys/bus/w1/devices/28-0000077aae57/w1_slave"
}
]
I have a get_line function which returns a Pymongo cursor:
>>> get_line()
<pymongo.cursor.Cursor object at 0x109162208>
I then define my marshmallow schemas:
from marshmallow import Schema, fields
class PointSchema(Schema):
x = fields.String()
y = fields.String()
class LineSchema(Schema):
sensor = fields.String()
data = fields.List(fields.Nested(PointSchema))
I use the following code within my flask endpoint to return the data to the client:
result = get_line()
output =
for line in result:
output.append({'sensor':line['name'], 'data': get_line(line['name'])})
return jsonify(LineSchema(many=True).dump(output))
I have also tried replacing LineSchema with:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
which results in the exact same JSON output. How should I be processing this to achieve the desired JSON? I'm ripping my hair out with this, but want to avoid changing the client's code to accommodate.
python pymongo marshmallow
What isget_linedoing? Or at the very lest what does result look like when you are running this so I have some test data.
– nerdlyist
Jan 2 at 20:07
@nerdlyist Thanks for your're comment, however I've posted my answer below.
– v25
Jan 2 at 20:37
add a comment |
I am having a problem with my Marshmallow output, whereby the JSON result is:
[
[
{
"data":[
{
"x":"2018-04-03 23:28:31.539895",
"y":15.937
}
],
"sensor":"/sys/bus/w1/devices/28-0000077aae57/w1_slave"
},
],
{
}
]
But I want it to look like:
[
{
"data":[
{
"x":"2018-04-03 23:37:40.612334",
"y":16.187
}
],
"sensor":"/sys/bus/w1/devices/28-0000077aae57/w1_slave"
}
]
I have a get_line function which returns a Pymongo cursor:
>>> get_line()
<pymongo.cursor.Cursor object at 0x109162208>
I then define my marshmallow schemas:
from marshmallow import Schema, fields
class PointSchema(Schema):
x = fields.String()
y = fields.String()
class LineSchema(Schema):
sensor = fields.String()
data = fields.List(fields.Nested(PointSchema))
I use the following code within my flask endpoint to return the data to the client:
result = get_line()
output =
for line in result:
output.append({'sensor':line['name'], 'data': get_line(line['name'])})
return jsonify(LineSchema(many=True).dump(output))
I have also tried replacing LineSchema with:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
which results in the exact same JSON output. How should I be processing this to achieve the desired JSON? I'm ripping my hair out with this, but want to avoid changing the client's code to accommodate.
python pymongo marshmallow
I am having a problem with my Marshmallow output, whereby the JSON result is:
[
[
{
"data":[
{
"x":"2018-04-03 23:28:31.539895",
"y":15.937
}
],
"sensor":"/sys/bus/w1/devices/28-0000077aae57/w1_slave"
},
],
{
}
]
But I want it to look like:
[
{
"data":[
{
"x":"2018-04-03 23:37:40.612334",
"y":16.187
}
],
"sensor":"/sys/bus/w1/devices/28-0000077aae57/w1_slave"
}
]
I have a get_line function which returns a Pymongo cursor:
>>> get_line()
<pymongo.cursor.Cursor object at 0x109162208>
I then define my marshmallow schemas:
from marshmallow import Schema, fields
class PointSchema(Schema):
x = fields.String()
y = fields.String()
class LineSchema(Schema):
sensor = fields.String()
data = fields.List(fields.Nested(PointSchema))
I use the following code within my flask endpoint to return the data to the client:
result = get_line()
output =
for line in result:
output.append({'sensor':line['name'], 'data': get_line(line['name'])})
return jsonify(LineSchema(many=True).dump(output))
I have also tried replacing LineSchema with:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
which results in the exact same JSON output. How should I be processing this to achieve the desired JSON? I'm ripping my hair out with this, but want to avoid changing the client's code to accommodate.
python pymongo marshmallow
python pymongo marshmallow
edited Jan 2 at 19:23
davidism
65.3k12175189
65.3k12175189
asked Jan 2 at 19:06
v25v25
472311
472311
What isget_linedoing? Or at the very lest what does result look like when you are running this so I have some test data.
– nerdlyist
Jan 2 at 20:07
@nerdlyist Thanks for your're comment, however I've posted my answer below.
– v25
Jan 2 at 20:37
add a comment |
What isget_linedoing? Or at the very lest what does result look like when you are running this so I have some test data.
– nerdlyist
Jan 2 at 20:07
@nerdlyist Thanks for your're comment, however I've posted my answer below.
– v25
Jan 2 at 20:37
What is
get_line doing? Or at the very lest what does result look like when you are running this so I have some test data.– nerdlyist
Jan 2 at 20:07
What is
get_line doing? Or at the very lest what does result look like when you are running this so I have some test data.– nerdlyist
Jan 2 at 20:07
@nerdlyist Thanks for your're comment, however I've posted my answer below.
– v25
Jan 2 at 20:37
@nerdlyist Thanks for your're comment, however I've posted my answer below.
– v25
Jan 2 at 20:37
add a comment |
1 Answer
1
active
oldest
votes
Okay I figured this out...
The correct marshmallow schema to use was:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
However, the reason for the strange JSON result in the form [ , {} ] was that LineSchema(many=True).dump(output) returns a MarshalObject in the form:
MarshalResult(data=[{'data': [{'y': 15.937, 'x': '2018-04-03 23:28:31.539895'}], 'sensor': '/sys/bus/w1/devices/28-0000077aae57/w1_slave'}], errors={})
So the correct syntax for the return was:
return jsonify(LineSchema(many=True).dump(output).data)
This github post actually suggests passing the instance and not the class into Nested, so it becomesNested (PointSchema(), many = true). Although I didn't find a significant performance increase in my code.
– v25
Jan 7 at 14:33
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%2f54011844%2fhow-to-get-desired-json-output-given-a-pymongo-cursor-with-marshmallow-schema%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
Okay I figured this out...
The correct marshmallow schema to use was:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
However, the reason for the strange JSON result in the form [ , {} ] was that LineSchema(many=True).dump(output) returns a MarshalObject in the form:
MarshalResult(data=[{'data': [{'y': 15.937, 'x': '2018-04-03 23:28:31.539895'}], 'sensor': '/sys/bus/w1/devices/28-0000077aae57/w1_slave'}], errors={})
So the correct syntax for the return was:
return jsonify(LineSchema(many=True).dump(output).data)
This github post actually suggests passing the instance and not the class into Nested, so it becomesNested (PointSchema(), many = true). Although I didn't find a significant performance increase in my code.
– v25
Jan 7 at 14:33
add a comment |
Okay I figured this out...
The correct marshmallow schema to use was:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
However, the reason for the strange JSON result in the form [ , {} ] was that LineSchema(many=True).dump(output) returns a MarshalObject in the form:
MarshalResult(data=[{'data': [{'y': 15.937, 'x': '2018-04-03 23:28:31.539895'}], 'sensor': '/sys/bus/w1/devices/28-0000077aae57/w1_slave'}], errors={})
So the correct syntax for the return was:
return jsonify(LineSchema(many=True).dump(output).data)
This github post actually suggests passing the instance and not the class into Nested, so it becomesNested (PointSchema(), many = true). Although I didn't find a significant performance increase in my code.
– v25
Jan 7 at 14:33
add a comment |
Okay I figured this out...
The correct marshmallow schema to use was:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
However, the reason for the strange JSON result in the form [ , {} ] was that LineSchema(many=True).dump(output) returns a MarshalObject in the form:
MarshalResult(data=[{'data': [{'y': 15.937, 'x': '2018-04-03 23:28:31.539895'}], 'sensor': '/sys/bus/w1/devices/28-0000077aae57/w1_slave'}], errors={})
So the correct syntax for the return was:
return jsonify(LineSchema(many=True).dump(output).data)
Okay I figured this out...
The correct marshmallow schema to use was:
class LineSchema(Schema):
sensor = fields.String()
data = fields.Nested(PointSchema, many=True)
However, the reason for the strange JSON result in the form [ , {} ] was that LineSchema(many=True).dump(output) returns a MarshalObject in the form:
MarshalResult(data=[{'data': [{'y': 15.937, 'x': '2018-04-03 23:28:31.539895'}], 'sensor': '/sys/bus/w1/devices/28-0000077aae57/w1_slave'}], errors={})
So the correct syntax for the return was:
return jsonify(LineSchema(many=True).dump(output).data)
answered Jan 2 at 20:10
v25v25
472311
472311
This github post actually suggests passing the instance and not the class into Nested, so it becomesNested (PointSchema(), many = true). Although I didn't find a significant performance increase in my code.
– v25
Jan 7 at 14:33
add a comment |
This github post actually suggests passing the instance and not the class into Nested, so it becomesNested (PointSchema(), many = true). Although I didn't find a significant performance increase in my code.
– v25
Jan 7 at 14:33
This github post actually suggests passing the instance and not the class into Nested, so it becomes
Nested (PointSchema(), many = true). Although I didn't find a significant performance increase in my code.– v25
Jan 7 at 14:33
This github post actually suggests passing the instance and not the class into Nested, so it becomes
Nested (PointSchema(), many = true). Although I didn't find a significant performance increase in my code.– v25
Jan 7 at 14:33
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%2f54011844%2fhow-to-get-desired-json-output-given-a-pymongo-cursor-with-marshmallow-schema%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
What is
get_linedoing? Or at the very lest what does result look like when you are running this so I have some test data.– nerdlyist
Jan 2 at 20:07
@nerdlyist Thanks for your're comment, however I've posted my answer below.
– v25
Jan 2 at 20:37