Change inner tuples in Json files with Python
I have a Json that looks like:
[
{
"id": "a199ffc8-86d3-4ed1-a5e1-87ed11b89e21",
"times": [
{
"start": 1543683600000,
"end": 1543791600000
},
{
"start": 1543827600000,
"end": 1543899600000
}
],
"priority": "P1"
},
{
"id": "e9d5ad69-806b-4c77-8be4-5be6d41db1c9",
"times": [
{
"start": 1543647600000,
"end": 1543683600000
},
{
"start": 1543719600000,
"end": 1543755600000
}
],
"priority": "P1"
}]
I am trying to change "start" and "end" time stamps but I can't figure out completly how to do it. U wrote something like:
from datetime import timedelta
with open('events.json') as json_file:
data = json.load(json_file, )
day_start = 28
for tuple in data:
tuple['start'] = (calendar.timegm(time.gmtime()) - int(timedelta(days = day_start).total_seconds()))*1000
tuple['end'] = (calendar.timegm(time.gmtime()) - int(timedelta(days = day_start-1).total_seconds()))*1000
day_start -= 2
with open('dataEvents.json', 'w') as outfile:
json.dump(data, outfile)
Any suugestion what to change in order to get into the right fields?
python json
add a comment |
I have a Json that looks like:
[
{
"id": "a199ffc8-86d3-4ed1-a5e1-87ed11b89e21",
"times": [
{
"start": 1543683600000,
"end": 1543791600000
},
{
"start": 1543827600000,
"end": 1543899600000
}
],
"priority": "P1"
},
{
"id": "e9d5ad69-806b-4c77-8be4-5be6d41db1c9",
"times": [
{
"start": 1543647600000,
"end": 1543683600000
},
{
"start": 1543719600000,
"end": 1543755600000
}
],
"priority": "P1"
}]
I am trying to change "start" and "end" time stamps but I can't figure out completly how to do it. U wrote something like:
from datetime import timedelta
with open('events.json') as json_file:
data = json.load(json_file, )
day_start = 28
for tuple in data:
tuple['start'] = (calendar.timegm(time.gmtime()) - int(timedelta(days = day_start).total_seconds()))*1000
tuple['end'] = (calendar.timegm(time.gmtime()) - int(timedelta(days = day_start-1).total_seconds()))*1000
day_start -= 2
with open('dataEvents.json', 'w') as outfile:
json.dump(data, outfile)
Any suugestion what to change in order to get into the right fields?
python json
1
What is your expected output? How do you want to see the "start" and "end" times?Also, you are dealing with a list dictionaries and not tuples.
– amanb
Jan 20 at 9:51
Looks like you are trying to solve the problem all at once - break the problem down: if you printdata
you will see that it is a list so you need to iterate on the list - then print each entry in the list, you will see it is a dictionary so you can work out which field to access, an so on.
– barny
Jan 20 at 10:03
add a comment |
I have a Json that looks like:
[
{
"id": "a199ffc8-86d3-4ed1-a5e1-87ed11b89e21",
"times": [
{
"start": 1543683600000,
"end": 1543791600000
},
{
"start": 1543827600000,
"end": 1543899600000
}
],
"priority": "P1"
},
{
"id": "e9d5ad69-806b-4c77-8be4-5be6d41db1c9",
"times": [
{
"start": 1543647600000,
"end": 1543683600000
},
{
"start": 1543719600000,
"end": 1543755600000
}
],
"priority": "P1"
}]
I am trying to change "start" and "end" time stamps but I can't figure out completly how to do it. U wrote something like:
from datetime import timedelta
with open('events.json') as json_file:
data = json.load(json_file, )
day_start = 28
for tuple in data:
tuple['start'] = (calendar.timegm(time.gmtime()) - int(timedelta(days = day_start).total_seconds()))*1000
tuple['end'] = (calendar.timegm(time.gmtime()) - int(timedelta(days = day_start-1).total_seconds()))*1000
day_start -= 2
with open('dataEvents.json', 'w') as outfile:
json.dump(data, outfile)
Any suugestion what to change in order to get into the right fields?
python json
I have a Json that looks like:
[
{
"id": "a199ffc8-86d3-4ed1-a5e1-87ed11b89e21",
"times": [
{
"start": 1543683600000,
"end": 1543791600000
},
{
"start": 1543827600000,
"end": 1543899600000
}
],
"priority": "P1"
},
{
"id": "e9d5ad69-806b-4c77-8be4-5be6d41db1c9",
"times": [
{
"start": 1543647600000,
"end": 1543683600000
},
{
"start": 1543719600000,
"end": 1543755600000
}
],
"priority": "P1"
}]
I am trying to change "start" and "end" time stamps but I can't figure out completly how to do it. U wrote something like:
from datetime import timedelta
with open('events.json') as json_file:
data = json.load(json_file, )
day_start = 28
for tuple in data:
tuple['start'] = (calendar.timegm(time.gmtime()) - int(timedelta(days = day_start).total_seconds()))*1000
tuple['end'] = (calendar.timegm(time.gmtime()) - int(timedelta(days = day_start-1).total_seconds()))*1000
day_start -= 2
with open('dataEvents.json', 'w') as outfile:
json.dump(data, outfile)
Any suugestion what to change in order to get into the right fields?
python json
python json
asked Jan 20 at 9:48
NumeratorNumerator
27231635
27231635
1
What is your expected output? How do you want to see the "start" and "end" times?Also, you are dealing with a list dictionaries and not tuples.
– amanb
Jan 20 at 9:51
Looks like you are trying to solve the problem all at once - break the problem down: if you printdata
you will see that it is a list so you need to iterate on the list - then print each entry in the list, you will see it is a dictionary so you can work out which field to access, an so on.
– barny
Jan 20 at 10:03
add a comment |
1
What is your expected output? How do you want to see the "start" and "end" times?Also, you are dealing with a list dictionaries and not tuples.
– amanb
Jan 20 at 9:51
Looks like you are trying to solve the problem all at once - break the problem down: if you printdata
you will see that it is a list so you need to iterate on the list - then print each entry in the list, you will see it is a dictionary so you can work out which field to access, an so on.
– barny
Jan 20 at 10:03
1
1
What is your expected output? How do you want to see the "start" and "end" times?Also, you are dealing with a list dictionaries and not tuples.
– amanb
Jan 20 at 9:51
What is your expected output? How do you want to see the "start" and "end" times?Also, you are dealing with a list dictionaries and not tuples.
– amanb
Jan 20 at 9:51
Looks like you are trying to solve the problem all at once - break the problem down: if you print
data
you will see that it is a list so you need to iterate on the list - then print each entry in the list, you will see it is a dictionary so you can work out which field to access, an so on.– barny
Jan 20 at 10:03
Looks like you are trying to solve the problem all at once - break the problem down: if you print
data
you will see that it is a list so you need to iterate on the list - then print each entry in the list, you will see it is a dictionary so you can work out which field to access, an so on.– barny
Jan 20 at 10:03
add a comment |
1 Answer
1
active
oldest
votes
If you want to turn back time by some amount of days for every start
and end
of your data you need to iterate over times
key of your dict's elements not just over all the elements of your dict.
import time
print('before:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
day_start = 28
for i in data:
for pair in i['times']:
pair['start'] = int((time.time() - day_start*60*60*24) * 1000)
pair['end'] = int((time.time() - day_start-1*60*60*24) * 1000)
day_start -= 2
print('nafter:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
Output
before:
{'end': 1543791600000, 'start': 1543683600000}
{'end': 1543899600000, 'start': 1543827600000}
{'end': 1543683600000, 'start': 1543647600000}
{'end': 1543755600000, 'start': 1543719600000}
after:
{'end': 1547892092406, 'start': 1545559320406}
{'end': 1547892094406, 'start': 1545732120406}
{'end': 1547892096406, 'start': 1545904920406}
{'end': 1547892098406, 'start': 1546077720406}
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%2f54275232%2fchange-inner-tuples-in-json-files-with-python%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
If you want to turn back time by some amount of days for every start
and end
of your data you need to iterate over times
key of your dict's elements not just over all the elements of your dict.
import time
print('before:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
day_start = 28
for i in data:
for pair in i['times']:
pair['start'] = int((time.time() - day_start*60*60*24) * 1000)
pair['end'] = int((time.time() - day_start-1*60*60*24) * 1000)
day_start -= 2
print('nafter:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
Output
before:
{'end': 1543791600000, 'start': 1543683600000}
{'end': 1543899600000, 'start': 1543827600000}
{'end': 1543683600000, 'start': 1543647600000}
{'end': 1543755600000, 'start': 1543719600000}
after:
{'end': 1547892092406, 'start': 1545559320406}
{'end': 1547892094406, 'start': 1545732120406}
{'end': 1547892096406, 'start': 1545904920406}
{'end': 1547892098406, 'start': 1546077720406}
add a comment |
If you want to turn back time by some amount of days for every start
and end
of your data you need to iterate over times
key of your dict's elements not just over all the elements of your dict.
import time
print('before:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
day_start = 28
for i in data:
for pair in i['times']:
pair['start'] = int((time.time() - day_start*60*60*24) * 1000)
pair['end'] = int((time.time() - day_start-1*60*60*24) * 1000)
day_start -= 2
print('nafter:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
Output
before:
{'end': 1543791600000, 'start': 1543683600000}
{'end': 1543899600000, 'start': 1543827600000}
{'end': 1543683600000, 'start': 1543647600000}
{'end': 1543755600000, 'start': 1543719600000}
after:
{'end': 1547892092406, 'start': 1545559320406}
{'end': 1547892094406, 'start': 1545732120406}
{'end': 1547892096406, 'start': 1545904920406}
{'end': 1547892098406, 'start': 1546077720406}
add a comment |
If you want to turn back time by some amount of days for every start
and end
of your data you need to iterate over times
key of your dict's elements not just over all the elements of your dict.
import time
print('before:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
day_start = 28
for i in data:
for pair in i['times']:
pair['start'] = int((time.time() - day_start*60*60*24) * 1000)
pair['end'] = int((time.time() - day_start-1*60*60*24) * 1000)
day_start -= 2
print('nafter:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
Output
before:
{'end': 1543791600000, 'start': 1543683600000}
{'end': 1543899600000, 'start': 1543827600000}
{'end': 1543683600000, 'start': 1543647600000}
{'end': 1543755600000, 'start': 1543719600000}
after:
{'end': 1547892092406, 'start': 1545559320406}
{'end': 1547892094406, 'start': 1545732120406}
{'end': 1547892096406, 'start': 1545904920406}
{'end': 1547892098406, 'start': 1546077720406}
If you want to turn back time by some amount of days for every start
and end
of your data you need to iterate over times
key of your dict's elements not just over all the elements of your dict.
import time
print('before:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
day_start = 28
for i in data:
for pair in i['times']:
pair['start'] = int((time.time() - day_start*60*60*24) * 1000)
pair['end'] = int((time.time() - day_start-1*60*60*24) * 1000)
day_start -= 2
print('nafter:')
print(*data[0]['times'], sep='n')
print(*data[1]['times'], sep='n')
Output
before:
{'end': 1543791600000, 'start': 1543683600000}
{'end': 1543899600000, 'start': 1543827600000}
{'end': 1543683600000, 'start': 1543647600000}
{'end': 1543755600000, 'start': 1543719600000}
after:
{'end': 1547892092406, 'start': 1545559320406}
{'end': 1547892094406, 'start': 1545732120406}
{'end': 1547892096406, 'start': 1545904920406}
{'end': 1547892098406, 'start': 1546077720406}
answered Jan 20 at 10:02
Filip MłynarskiFilip Młynarski
1,7711413
1,7711413
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%2f54275232%2fchange-inner-tuples-in-json-files-with-python%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
What is your expected output? How do you want to see the "start" and "end" times?Also, you are dealing with a list dictionaries and not tuples.
– amanb
Jan 20 at 9:51
Looks like you are trying to solve the problem all at once - break the problem down: if you print
data
you will see that it is a list so you need to iterate on the list - then print each entry in the list, you will see it is a dictionary so you can work out which field to access, an so on.– barny
Jan 20 at 10:03