Skip to content
Advertisement

slow update of large JSON File

I have a large JSON file that contains serialized json dicts. I am trying to iterate this file and update based on the contents of another dict. The JSON file looks similar to:

JavaScript

The function below, iterates each line of the JSON file and creates a “key_value” that’s a concatenation of item and lookup key value pairs.

JavaScript

The function seems to run really slow and freezes half way through.

JavaScript

Advertisement

Answer

There are a lot of strange things in your code, I removed several of them in an attempt to give you something that isn’t slow.

Start with something that separates your logic and lets you avoid storing stuff in memory. This reads the tmp file at the end and replays it over, but you could also just do a file-level rename if you wanted (or keep the new one, etc.)

JavaScript

This leaves convert and writeLine to be defined. Your original logic has strange behavior but I think it is supposed to look like this

JavaScript

A testable main function might look like this:

JavaScript

which we would expect to generate a file like

JavaScript

since the second element of data has no matching tags, so will be skipped.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement