1

I have a json file; I need to remove the id key from the content, which I can do with my code.

Now I want to print each line of the json file in a new file and use the name filed in my json for the file name.

My json file ex:

{"categories":["Test"],"indications":[{"@class":"=indication.BuildLogIndication","pattern":".*TypeError .*"},{"@class":"model.indication.BuildLogIndication","pattern":".*LoadError .*"}],"modifications":[{"time":{"$date":"2015-10-08T20:01:54.075Z"}},{"user":"user1","time":{"$date":"2015-03-04T18:38:58.123Z"}},{"user":"user2","time":{"$date":"2014-11-13T01:54:13.906Z"}},{"time":{"$date":"2014-09-02T18:48:05.000Z"}}],"lastOccurred":{"$date":"2017-01-25T20:05:17.180Z"}}
{"pattern":".*look for this string.*"}],"modifications":[{"time":{"$date":"2014-09-02T18:52:20.000Z"}}],"lastOccurred":{"$date":"2014-11-04T00:43:32.945Z"},"_removed":{"timestamp":{"$date":"2014-11-13T01:52:44.346Z"},"by":"user3"},"active":false}

Code for removing id:

import json
import sys
import re
import fileinput

infile = "failure.json"
outfile = "failure1.json"

fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
    for word in line:
        line = re.sub("\"_id.*?},","", line)
    fout.write(line)
    file.write("%d\n" % n)
fin.close()
fout.close()
Jan Tamm
  • 117
  • 2
  • 11
  • 1
    You've imported `json` package, but you're not using it. You should, it's great. Get your string from file and then use `json.loads()` to load the string into a json object. From there, you can get each element of the json object with `for key in json_object`. – mindfolded Oct 03 '18 at 18:36
  • @mindfolded would you please write it as a solution.. much appropriated.. I am also new in Python..Thanks in advanced – Jan Tamm Oct 03 '18 at 18:44
  • Possible duplicate [loading-and-parsing-a-json-file-with-multiple-json-objects-in-python](https://stackoverflow.com/questions/12451431/loading-and-parsing-a-json-file-with-multiple-json-objects-in-python) and [delete-an-element-from-a-dictionary](https://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary) – stovfl Oct 03 '18 at 19:05
  • @jantamm It has been written as a solution. – mindfolded Oct 05 '18 at 18:35

3 Answers3

1

For deletion you could use something like this:

import json
import sys
import re
import fileinput

with open('failure.json') as data_file:
    data = json.load(data_file)
    del data['_id']


with open('failure2.json', 'w') as data_file:
    data = json.dump(data, data_file)

and in order to create file with id value, just parse data object, and value of id node

kosist
  • 2,868
  • 2
  • 17
  • 30
0

You sample input show a json object on each line.

So my solution reads each line and converts it to a python dict (using json.loads()), removes the desired key from the dict (using dict.pop() to fail silently if the key is not present) and converts it back to a string (using json.dumps()), which is then written to the new file.

import json

infile = "failure.json"
outfile = "failure1.json"
key = '_id'

with open(infile) as f_read:
    with open(outfile, 'w') as f_write:
        for line in f_read:
            line = line.strip()
            if len(line) > 0:
                try:
                    elem = json.loads(line)
                    elem.pop(key, None)
                    f_write.write('{}\n'.format(json.dumps(elem)))
                except json.JSONDecodeError:
                    pass

EDIT: apparently each json line should go into a separate new file, according to OPs comments. That could be done like this, for example:

import json

infile = "failure.json"
key_to_remove = '_id'

with open(infile) as f_read:
    for line in f_read:
        line = line.strip()
        if len(line) > 0:
            try:
                elem = json.loads(line)
                elem.pop(key_to_remove, None)

                outfile = '{}.json'.format(elem['name'])      # this may raise KeyError
                with open(outfile, 'w') as f_write:
                    f_write.write('{}\n'.format(json.dumps(elem)))
            except json.JSONDecodeError:
                pass
Ralf
  • 16,086
  • 4
  • 44
  • 68
  • Thank you but I want to print each line of the json file in a new file and use the name filed in my json for the file name. – Jan Tamm Oct 03 '18 at 19:34
  • which key in the json data contains the new file name? – Ralf Oct 03 '18 at 19:35
  • name >> "name":"Ruby" – Jan Tamm Oct 03 '18 at 19:41
  • Thank you, quick question, how we can check for special character in name field like '/' and replace them with ' _' before generating the file? Thanks in advance – Jan Tamm Oct 03 '18 at 20:04
  • @JanTamm you could do `elem['name'].replace('/', '_')` or something similar – Ralf Oct 03 '18 at 20:07
  • question, how can I only have the following in my names ? a-zA-Z0-9_- like character/numbers and _ - and replace anything else with' _ ', Thank you again – Jan Tamm Oct 03 '18 at 20:42
  • @JanTamm you can look into [this question](https://stackoverflow.com/q/1276764/9225671) for that, or ask a new one. Dont forget to upvote if my answer helped you. – Ralf Oct 03 '18 at 20:45
0

You've imported the json package, but you're not using it. You should, it's great.

Get your string from file and then use json.loads() to load the string into a json object. From there, you can get each element of the json object with for key in json_object.

mindfolded
  • 216
  • 1
  • 8