1

I have a file with this type of structure:

{ 
  "key" : "A",
  "description" : "1",
  "uninterestingInformation" : "whatever"

}
{ 
  "key" : "B",
  "description" : "2",
  "uninterestingInformation" : "whatever"

}
{ 
  "key" : "C",
  "description" : "3",
  "uninterestingInformation" : "whatever"

}

I want to build a dictionary in Python that contains the key as key and the description as value. I have more fields, but just the 2 of them are interesting for me.

This file is not exactly a .json file, is a file with a lot of similar json objects.

json.loads is not working, obviously.

Any suggestion on how to read the data?

I've already read this post, but my json object is not on one line...

EDIT:

If it wasn't clear in my explanations, the example is quite accurate, I have a lot of similar JSON objects, one after another, separated by new line (\n), with no comma. So, overall the file is not a valid JSON file, while each object is a valid JSON object.

The solution I've applied finally was:

api_key_file = open('mongo-config.json').read()
api_key_file = '[' + api_key_file + ']'
api_key_file= api_key_file.replace("}\n{", "},\n{")
api_key_data = json.loads(api_key_file)
api_key_description = {}
for data in api_key_data:
    api_key_description[data['apiKey']] = data['description']

It worked well for my situation. There are maybe better ways of doing this explained in the comments bellow.

2 Answers2

1

Another option would be to use the literal_eval function from the ast module, after making the necessary changes so that it fits the format of a valid type:

from ast import literal_eval

inJson = '''{ 
  "key" : "A"
  "description" : "1"
  "uninterestingInformation" : "whatever"

}
{ 
  "key" : "B"
  "description" : "2"
  "uninterestingInformation" : "whatever"

}
{ 
  "key" : "C"
  "description" : "3"
  "uninterestingInformation" : "whatever"

}'''

inJson = "[" + inJson.replace("}", "},")[:-1] + "]"
inJson = inJson.replace("\"\n  ","\",")


newObject = literal_eval(inJson)
print(newObject)

Output:

[{'key': 'A', 'description': '1', 'uninterestingInformation': 'whatever'}, {'key': 'B', 'description': '2', 'uninterestingInformation': 'whatever'}, {'key': 'C', 'description': '3', 'uninterestingInformation': 'whatever'}]
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
-1

You can use re.split to split the file content into appropriate JSON strings for parsing:

import re
import json
j='''{ 
  "key" : "A",
  "description" : "1",
  "uninterestingInformation" : "whatever"

}
{ 
  "key" : "B",
  "description" : "2",
  "uninterestingInformation" : "whatever"

}
{ 
  "key" : "C",
  "description" : "3",
  "uninterestingInformation" : "whatever"

}'''
print(list(map(json.loads, re.split(r'(?<=})\n(?={)', j))))

This outputs:

[{'key': 'A', 'description': '1', 'uninterestingInformation': 'whatever'}, {'key': 'B', 'description': '2', 'uninterestingInformation': 'whatever'}, {'key': 'C', 'description': '3', 'uninterestingInformation': 'whatever'}]
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • You've put in commas though. That is not the data OP has, theirs is newline delimited. – FHTMitchell Aug 01 '18 at 11:32
  • I'm basing my assumptions on the OP's statement of "This file is not exactly a .json file, is a file with a lot of similar json objects.", which means that the file has several valid JSON objects, and the lack of commas in his example is just due to his quick typing for illustration purposes only. – blhsing Aug 01 '18 at 12:14
  • That is a poor assumption. I'd say its much better to assume that OP's example is accurate. – FHTMitchell Aug 01 '18 at 12:15
  • The OP's example uses fake keys and values, so it's quite apparent that he typed everything manually, so his description of the file should be taken more seriously than his typed structure. – blhsing Aug 01 '18 at 12:16
  • The example was proper. Maybe I didn't explain it well. I had a lot of JSON objects one after another, but not put in a list, so the entire file was not a valid JSON file, but each object separated was a proper JSON object. I think I should try to be more clear next time. The OP is a girl, btw :D but it doesn't matter. – Arhiliuc Cristina Aug 01 '18 at 13:48
  • @ArhiliucCristina If your example was proper then each object is NOT a proper JSON object because of the missing commas. Calling them "json objects" is thus very misleading, and hence the debate here. – blhsing Aug 01 '18 at 13:51
  • Ok, my fault, you are right. I forgot to put the commas in my example. Now I see what are you talking about. My sincere apologies. No comma between objects, but comma in the objects, I edit them now – Arhiliuc Cristina Aug 01 '18 at 13:55
  • @ArhiliucCristina I see. My assumption was correct after all then. Thanks for the clarification. – blhsing Aug 01 '18 at 13:56