json dumps not including attribute name
up vote
0
down vote
favorite
I am writing a python lambda function that reads in a json file from s3 and then will take one of the nodes and send it to another lambda function. Here is my code:
The json snippet I want
"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
}
step 1 – convert JSON to python objects for processing
note: these I got from another Stack Overflow guru - thanks!!
def _json_object_hook(d): return namedtuple('X', d.keys())(*d.values())
def json2obj(data): return json.loads(data, object_hook=_json_object_hook)
routes = json2obj(jsonText)
step 2 - I then traverse the python objects and find the json I need and dump it
for jobstream in jobstreams:
x = json.dumps(jobstream, ensure_ascii=False)
Howeever, when I print it out, I only have the values not the attributes. Why is that?
print(json.dumps(jobstream, ensure_ascii=False))
yields
["team-summary", "aaa-bbb", "team-summary.json"]
python json
|
show 2 more comments
up vote
0
down vote
favorite
I am writing a python lambda function that reads in a json file from s3 and then will take one of the nodes and send it to another lambda function. Here is my code:
The json snippet I want
"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
}
step 1 – convert JSON to python objects for processing
note: these I got from another Stack Overflow guru - thanks!!
def _json_object_hook(d): return namedtuple('X', d.keys())(*d.values())
def json2obj(data): return json.loads(data, object_hook=_json_object_hook)
routes = json2obj(jsonText)
step 2 - I then traverse the python objects and find the json I need and dump it
for jobstream in jobstreams:
x = json.dumps(jobstream, ensure_ascii=False)
Howeever, when I print it out, I only have the values not the attributes. Why is that?
print(json.dumps(jobstream, ensure_ascii=False))
yields
["team-summary", "aaa-bbb", "team-summary.json"]
python json
1
your "json snip" is not a valid/complete JSON
– Azat Ibrakov
Nov 10 at 5:26
I don't see an lambda functions? what out does your script produce?
– Red Cricket
Nov 10 at 5:32
Yourobject_hook=
is causing JSON objects to be loaded as Python objects that have no exact JSON equivalent. If you just try to dump them back to JSON, of course there's going to be data loss. You'd need to provide the opposite transformation of these objects - but that does not seem to be easily done here:json.dumps()
does have adefault=
option that is sort of the opposite ofobject_hook=
, but it only applies to objects with no JSON equivalent at all, which isn't the case for namedtuples.
– jasonharper
Nov 10 at 5:52
thanks everyone!! azat, yes-it was just a snip....Red Cricket, the lambda function is just getting part of the json file to start another process.. dani, your code is similar to what i have didnt want to post entire code as didnt think that would be protocol.. @Jasonharper,...hmm, I was wondering about the fancy object hook thing i am using. candidly, i got that from another person but dont quite understand it. it gives me the ability to create a python object with multiple trees in the json. I tried just Json.loads and it gives me a dict error when i get to a list... so i am stuck.
– deant
Nov 10 at 6:06
should i not use the NamedTuple technique? How else would I create a python object sourced from a Json stream with multiple hierarchical lists and complex json structures? any suggestions or links would be appreciated by this newbie...
– deant
Nov 10 at 6:11
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am writing a python lambda function that reads in a json file from s3 and then will take one of the nodes and send it to another lambda function. Here is my code:
The json snippet I want
"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
}
step 1 – convert JSON to python objects for processing
note: these I got from another Stack Overflow guru - thanks!!
def _json_object_hook(d): return namedtuple('X', d.keys())(*d.values())
def json2obj(data): return json.loads(data, object_hook=_json_object_hook)
routes = json2obj(jsonText)
step 2 - I then traverse the python objects and find the json I need and dump it
for jobstream in jobstreams:
x = json.dumps(jobstream, ensure_ascii=False)
Howeever, when I print it out, I only have the values not the attributes. Why is that?
print(json.dumps(jobstream, ensure_ascii=False))
yields
["team-summary", "aaa-bbb", "team-summary.json"]
python json
I am writing a python lambda function that reads in a json file from s3 and then will take one of the nodes and send it to another lambda function. Here is my code:
The json snippet I want
"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
}
step 1 – convert JSON to python objects for processing
note: these I got from another Stack Overflow guru - thanks!!
def _json_object_hook(d): return namedtuple('X', d.keys())(*d.values())
def json2obj(data): return json.loads(data, object_hook=_json_object_hook)
routes = json2obj(jsonText)
step 2 - I then traverse the python objects and find the json I need and dump it
for jobstream in jobstreams:
x = json.dumps(jobstream, ensure_ascii=False)
Howeever, when I print it out, I only have the values not the attributes. Why is that?
print(json.dumps(jobstream, ensure_ascii=False))
yields
["team-summary", "aaa-bbb", "team-summary.json"]
python json
python json
edited Nov 10 at 6:48
petezurich
3,44881633
3,44881633
asked Nov 10 at 5:20
deant
11
11
1
your "json snip" is not a valid/complete JSON
– Azat Ibrakov
Nov 10 at 5:26
I don't see an lambda functions? what out does your script produce?
– Red Cricket
Nov 10 at 5:32
Yourobject_hook=
is causing JSON objects to be loaded as Python objects that have no exact JSON equivalent. If you just try to dump them back to JSON, of course there's going to be data loss. You'd need to provide the opposite transformation of these objects - but that does not seem to be easily done here:json.dumps()
does have adefault=
option that is sort of the opposite ofobject_hook=
, but it only applies to objects with no JSON equivalent at all, which isn't the case for namedtuples.
– jasonharper
Nov 10 at 5:52
thanks everyone!! azat, yes-it was just a snip....Red Cricket, the lambda function is just getting part of the json file to start another process.. dani, your code is similar to what i have didnt want to post entire code as didnt think that would be protocol.. @Jasonharper,...hmm, I was wondering about the fancy object hook thing i am using. candidly, i got that from another person but dont quite understand it. it gives me the ability to create a python object with multiple trees in the json. I tried just Json.loads and it gives me a dict error when i get to a list... so i am stuck.
– deant
Nov 10 at 6:06
should i not use the NamedTuple technique? How else would I create a python object sourced from a Json stream with multiple hierarchical lists and complex json structures? any suggestions or links would be appreciated by this newbie...
– deant
Nov 10 at 6:11
|
show 2 more comments
1
your "json snip" is not a valid/complete JSON
– Azat Ibrakov
Nov 10 at 5:26
I don't see an lambda functions? what out does your script produce?
– Red Cricket
Nov 10 at 5:32
Yourobject_hook=
is causing JSON objects to be loaded as Python objects that have no exact JSON equivalent. If you just try to dump them back to JSON, of course there's going to be data loss. You'd need to provide the opposite transformation of these objects - but that does not seem to be easily done here:json.dumps()
does have adefault=
option that is sort of the opposite ofobject_hook=
, but it only applies to objects with no JSON equivalent at all, which isn't the case for namedtuples.
– jasonharper
Nov 10 at 5:52
thanks everyone!! azat, yes-it was just a snip....Red Cricket, the lambda function is just getting part of the json file to start another process.. dani, your code is similar to what i have didnt want to post entire code as didnt think that would be protocol.. @Jasonharper,...hmm, I was wondering about the fancy object hook thing i am using. candidly, i got that from another person but dont quite understand it. it gives me the ability to create a python object with multiple trees in the json. I tried just Json.loads and it gives me a dict error when i get to a list... so i am stuck.
– deant
Nov 10 at 6:06
should i not use the NamedTuple technique? How else would I create a python object sourced from a Json stream with multiple hierarchical lists and complex json structures? any suggestions or links would be appreciated by this newbie...
– deant
Nov 10 at 6:11
1
1
your "json snip" is not a valid/complete JSON
– Azat Ibrakov
Nov 10 at 5:26
your "json snip" is not a valid/complete JSON
– Azat Ibrakov
Nov 10 at 5:26
I don't see an lambda functions? what out does your script produce?
– Red Cricket
Nov 10 at 5:32
I don't see an lambda functions? what out does your script produce?
– Red Cricket
Nov 10 at 5:32
Your
object_hook=
is causing JSON objects to be loaded as Python objects that have no exact JSON equivalent. If you just try to dump them back to JSON, of course there's going to be data loss. You'd need to provide the opposite transformation of these objects - but that does not seem to be easily done here: json.dumps()
does have a default=
option that is sort of the opposite of object_hook=
, but it only applies to objects with no JSON equivalent at all, which isn't the case for namedtuples.– jasonharper
Nov 10 at 5:52
Your
object_hook=
is causing JSON objects to be loaded as Python objects that have no exact JSON equivalent. If you just try to dump them back to JSON, of course there's going to be data loss. You'd need to provide the opposite transformation of these objects - but that does not seem to be easily done here: json.dumps()
does have a default=
option that is sort of the opposite of object_hook=
, but it only applies to objects with no JSON equivalent at all, which isn't the case for namedtuples.– jasonharper
Nov 10 at 5:52
thanks everyone!! azat, yes-it was just a snip....Red Cricket, the lambda function is just getting part of the json file to start another process.. dani, your code is similar to what i have didnt want to post entire code as didnt think that would be protocol.. @Jasonharper,...hmm, I was wondering about the fancy object hook thing i am using. candidly, i got that from another person but dont quite understand it. it gives me the ability to create a python object with multiple trees in the json. I tried just Json.loads and it gives me a dict error when i get to a list... so i am stuck.
– deant
Nov 10 at 6:06
thanks everyone!! azat, yes-it was just a snip....Red Cricket, the lambda function is just getting part of the json file to start another process.. dani, your code is similar to what i have didnt want to post entire code as didnt think that would be protocol.. @Jasonharper,...hmm, I was wondering about the fancy object hook thing i am using. candidly, i got that from another person but dont quite understand it. it gives me the ability to create a python object with multiple trees in the json. I tried just Json.loads and it gives me a dict error when i get to a list... so i am stuck.
– deant
Nov 10 at 6:06
should i not use the NamedTuple technique? How else would I create a python object sourced from a Json stream with multiple hierarchical lists and complex json structures? any suggestions or links would be appreciated by this newbie...
– deant
Nov 10 at 6:11
should i not use the NamedTuple technique? How else would I create a python object sourced from a Json stream with multiple hierarchical lists and complex json structures? any suggestions or links would be appreciated by this newbie...
– deant
Nov 10 at 6:11
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
I'm assuming your full json file looks somewhat like what I have in my example
import json
js = {"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
},
{
"jobname": "team-2222",
"bucket": "aaa-2222",
"key": "team-222.json"
}
]}
def extract_by_jobname(jobname):
for d in js['jobstreams']:
if d['jobname'] == jobname:
return d
json.dumps(extract_by_jobname("team-summary"))
# '{"jobname": "team-summary", "bucket": "aaa-bbb", "key": "team-summary.json"}'
add a comment |
up vote
0
down vote
I ended up creating a new Dictionary from the list that the json.dumps gave me.
["team-summary", "aaa-bbb", "team-summary.json"]
once i had the new dictionary (that is flat), then i converted that to json.... probably not the most efficient approach but i have other fish to fry. THANKS to all for your help!
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',
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%2f53236210%2fjson-dumps-not-including-attribute-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I'm assuming your full json file looks somewhat like what I have in my example
import json
js = {"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
},
{
"jobname": "team-2222",
"bucket": "aaa-2222",
"key": "team-222.json"
}
]}
def extract_by_jobname(jobname):
for d in js['jobstreams']:
if d['jobname'] == jobname:
return d
json.dumps(extract_by_jobname("team-summary"))
# '{"jobname": "team-summary", "bucket": "aaa-bbb", "key": "team-summary.json"}'
add a comment |
up vote
0
down vote
I'm assuming your full json file looks somewhat like what I have in my example
import json
js = {"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
},
{
"jobname": "team-2222",
"bucket": "aaa-2222",
"key": "team-222.json"
}
]}
def extract_by_jobname(jobname):
for d in js['jobstreams']:
if d['jobname'] == jobname:
return d
json.dumps(extract_by_jobname("team-summary"))
# '{"jobname": "team-summary", "bucket": "aaa-bbb", "key": "team-summary.json"}'
add a comment |
up vote
0
down vote
up vote
0
down vote
I'm assuming your full json file looks somewhat like what I have in my example
import json
js = {"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
},
{
"jobname": "team-2222",
"bucket": "aaa-2222",
"key": "team-222.json"
}
]}
def extract_by_jobname(jobname):
for d in js['jobstreams']:
if d['jobname'] == jobname:
return d
json.dumps(extract_by_jobname("team-summary"))
# '{"jobname": "team-summary", "bucket": "aaa-bbb", "key": "team-summary.json"}'
I'm assuming your full json file looks somewhat like what I have in my example
import json
js = {"jobstreams": [
{
"jobname": "team-summary",
"bucket": "aaa-bbb",
"key": "team-summary.json"
},
{
"jobname": "team-2222",
"bucket": "aaa-2222",
"key": "team-222.json"
}
]}
def extract_by_jobname(jobname):
for d in js['jobstreams']:
if d['jobname'] == jobname:
return d
json.dumps(extract_by_jobname("team-summary"))
# '{"jobname": "team-summary", "bucket": "aaa-bbb", "key": "team-summary.json"}'
answered Nov 10 at 5:30
Dani G
427411
427411
add a comment |
add a comment |
up vote
0
down vote
I ended up creating a new Dictionary from the list that the json.dumps gave me.
["team-summary", "aaa-bbb", "team-summary.json"]
once i had the new dictionary (that is flat), then i converted that to json.... probably not the most efficient approach but i have other fish to fry. THANKS to all for your help!
add a comment |
up vote
0
down vote
I ended up creating a new Dictionary from the list that the json.dumps gave me.
["team-summary", "aaa-bbb", "team-summary.json"]
once i had the new dictionary (that is flat), then i converted that to json.... probably not the most efficient approach but i have other fish to fry. THANKS to all for your help!
add a comment |
up vote
0
down vote
up vote
0
down vote
I ended up creating a new Dictionary from the list that the json.dumps gave me.
["team-summary", "aaa-bbb", "team-summary.json"]
once i had the new dictionary (that is flat), then i converted that to json.... probably not the most efficient approach but i have other fish to fry. THANKS to all for your help!
I ended up creating a new Dictionary from the list that the json.dumps gave me.
["team-summary", "aaa-bbb", "team-summary.json"]
once i had the new dictionary (that is flat), then i converted that to json.... probably not the most efficient approach but i have other fish to fry. THANKS to all for your help!
answered Nov 10 at 19:32
deant
11
11
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53236210%2fjson-dumps-not-including-attribute-name%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
1
your "json snip" is not a valid/complete JSON
– Azat Ibrakov
Nov 10 at 5:26
I don't see an lambda functions? what out does your script produce?
– Red Cricket
Nov 10 at 5:32
Your
object_hook=
is causing JSON objects to be loaded as Python objects that have no exact JSON equivalent. If you just try to dump them back to JSON, of course there's going to be data loss. You'd need to provide the opposite transformation of these objects - but that does not seem to be easily done here:json.dumps()
does have adefault=
option that is sort of the opposite ofobject_hook=
, but it only applies to objects with no JSON equivalent at all, which isn't the case for namedtuples.– jasonharper
Nov 10 at 5:52
thanks everyone!! azat, yes-it was just a snip....Red Cricket, the lambda function is just getting part of the json file to start another process.. dani, your code is similar to what i have didnt want to post entire code as didnt think that would be protocol.. @Jasonharper,...hmm, I was wondering about the fancy object hook thing i am using. candidly, i got that from another person but dont quite understand it. it gives me the ability to create a python object with multiple trees in the json. I tried just Json.loads and it gives me a dict error when i get to a list... so i am stuck.
– deant
Nov 10 at 6:06
should i not use the NamedTuple technique? How else would I create a python object sourced from a Json stream with multiple hierarchical lists and complex json structures? any suggestions or links would be appreciated by this newbie...
– deant
Nov 10 at 6:11