0

I am working with JSON files in python. I have keys in a list, for each key I retrieve a JSON record, which I store in a file. Each JSON record contains that key inside.

In a separate process, I need to read that JSON file and retrieve each record, get key and do something else.

The problem is I cannot read JSON lines from file I stored this way:

dataFile = open(output_file, "w")
for line in keys_list:
   json_line = fetch_json_str(line)
   data = simplejson.loads(json_line)
   dataFile.write(simplejson.dumps(data, sort_keys = True))

It reads all into one line, and the length of returned list is 0, len(json_lines).

  json_lines = [line.strip() for line in open(tmp_load_file)]
  for line in json_lines
      data = simplejson.loads(simplejson.dumps(line))

What do I do wrong? Is there any way to get back JSON lines without changing the way I stored them, because that will call for re-processing all the json files I stored this way.

1 Answer 1

1

You need to add a line separator when you write the json string

dataFile.write(simplejson.dumps(data, sort_keys = True) + '\n')

Otherwise, you just get all of the json records in a single line and the json parser can't figure it out.

Sign up to request clarification or add additional context in comments.

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.