1

Hey i have a json file eg: [ { "Reporter": "abc", "Created": "2015-05-28 11:29:16", "Assignee": "ABC", "Key": "JIRA-123" }, { "Reporter": "def", "Created": "2015-05-28 11:29:16", "Assignee": "DEF", "Key": "JIRA-234" } ]

in the above format, now i need to add some more entries in to this file from a dynamically generated data lets say the below data

{
    "Reporter": "xyz",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "XYZ",
    "Key": "JIRA-456"
},
{
    "Reporter": "utf",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "UTF",
    "Key": "JIRA-678"
}

but when i update this data into the above json file its again going to have a seperate list rather than combining.

like:

[
{
    "Reporter": "abc",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "ABC",
    "Key": "JIRA-123"
},
{
    "Reporter": "def",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "DEF",
    "Key": "JIRA-234"
}
]
[
{
    "Reporter": "xyz",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "XYZ",
    "Key": "JIRA-456"
},
{
    "Reporter": "utf",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "UTF",
    "Key": "JIRA-678"
}
]

but what i want is in the below format

[
{
    "Reporter": "abc",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "ABC",
    "Key": "JIRA-123"
},
{
    "Reporter": "def",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "DEF",
    "Key": "JIRA-234"
},
{
    "Reporter": "xyz",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "XYZ",
    "Key": "JIRA-456"
},
{
    "Reporter": "utf",
    "Created": "2015-05-28 11:29:16",
    "Assignee": "UTF",
    "Key": "JIRA-678"
}
]

i tried with the modes r+, and a+ but i didn't get what i want.

with open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'static', 'result', file_name + '_issues.json')), 'r+') as nInfo:
  nInfo.write(json.dumps(file_data,indent=4, separators=(',', ': ')))

Can anyone please help in achieving this.

Txs! VK

2
  • I guess you should load data from JSON, update it and rewrite file. I know it isn't efficient though Commented May 28, 2015 at 19:33
  • I'm not much skilled in python and you code shows only the writing. So I think that, you only append the new data (serialized as text) behind the text that's allready in your file. At first I would read the old data into an array add your object to that array and overwrite the data in the data file with the text of the serialized array. If you already read the array in, then put that code in the question. Commented May 28, 2015 at 19:42

2 Answers 2

1

Assuming your json file contains a list of objects:

First load your json from the file

with open('file.json') as f:
    oldjson = json.load(f)

Then update the json with the new dict and overwrite the file

oldjson.append(list_of_dicts)
with open('file.json', 'w') as f:
    f.write(json.dumps(oldjson, indent=4, separators=(',', ': ')))
Sign up to request clarification or add additional context in comments.

2 Comments

Reading the entire json data first is not the efficient one as its a very huge file as my json file grows continuously i'm sure its going to hit memory issue, the above pasted is a sample so is there any other way rather than file.seek() and append the data from a particular position?
You're right, this solution may encounter performance/memory issues if the JSON file gets really large. Another solution would be what you suggested: seeking to the end of JSON file and appending the new data there. If you're not absolutely constrained to one JSON file, you could also limit the JSON file size and use multiple files.
0

I figured out a way.

with open(os.path.abspath(os.path.join(os.path.dirname(__file__),'static', 'result', file_name + '_issues.json')), 'r+') as nInfo:
  count = 0
  for data in file_data:
      count += 1
      if count == 1:
        nInfo.seek(-2, 2)
      if file_data.index(data) != len(file_data):
        nInfo.write(',')
      nInfo.write(json.dumps(data, default=lambda a: '[%s,%s]'(str(type(a)), a.pk)))
  nInfo.write(']')
  nInfo.write(' ')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.