Reputation: 574
I have a huge text file.
line 1
line 2
line 3
...
I have converted it into an array of lists:
[['String 1'],['String 2'],['String 3'],['String 4'],['String 5'],
['String 6'],['String 7'],['String 8'],['String 9'],['String 9'],
['String 10'], ...]
I want to convert this list to JSON objects, like this:
[{'title1': 'String 1', 'title2': 'String 2', ... , 'title7': 'String 7'},
{'title1': 'String 8', ..., 'title7': 'String 14'}, ...]
I am not sure how to do it. Any help.?
Upvotes: 12
Views: 118819
Reputation: 1
I just changed my list to dict using dict()
and then
import json
json.dump(data,f)
where f
is my file name to store the json data.
Upvotes: 0
Reputation: 310
ll = [['String 1'],['String 2']]
# 1- Unlist
ll = sum(ll, [])
# 2- Make list to json
list_with_dics = [{"title"+str(i+1): val} for i, val in enumerate(ll)]
Result:[{'title1': 'String 1'}, {'title1': 'String 2'}]
Upvotes: 0
Reputation: 31
Define a class as custom type before serializing. Then set this in a loop in the main class and return using json.dumps()
import json
class CustomType:
def __init__(self, title, text):
self.title = title
self.text = text
def toJSON(self):
'''
Serialize the object custom object
'''
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
in main class:
def get_json_data():
'''
Convert string array to json array
'''
result = []
for item in data:
obj = CustomType("title(n)",item)
result.append(json.loads(obj.toJSON()))
return json.dumps(result)
Upvotes: 0
Reputation: 123531
As @alecxe pointed out, you need to divide the array of lists you got from the file into groups of values with 7 or fewer elements. You can then take a list of any 7 titles you want and use them as keys to create the dictionary of each json object in the final list.
try:
from itertools import izip
except ImportError: # Python 3
izip = zip
try:
xrange
except NameError: # Python 3
xrange = range
def grouper(n, sequence):
for i in xrange(0, len(sequence), n):
yield sequence[i:i+n]
data = [['String 1'],['String 2'],['String 3'],['String 4'],['String 5'],
['String 6'],['String 7'],['String 8'],['String 9'],['String 9'],
['String 10']]
titles = ['title1', 'title2', 'title3', 'title4', 'title5', 'title6', 'title7']
values = [e[0] for g in grouper(7, data) for e in g]
keys = (titles[i%7] for i in xrange(len(values)))
objs = [dict(g) for g in grouper(7, list(izip(keys, values)))]
print(objs)
Output:
[{'title1': 'String 1', 'title2': 'String 2', 'title3': 'String 3',
'title4': 'String 4', 'title5': 'String 5', 'title6': 'String 6',
'title7': 'String 7'}, {'title1': 'String 8', 'title2': 'String 9',
'title3': 'String 9', 'title4': 'String 10'}]
Upvotes: 1
Reputation: 413
Just adding onto alexce's response, you can easily convert the restructured data into JSON:
import json
json.dumps(result)
There are some potential security concerns with top-level arrays. I'm not sure if they're still valid with modern browsers, but you may want to consider wrapping it in an object.
import json
json.dumps({'results': result})
Upvotes: 21
Reputation: 474231
To solve this, you need to split the input list into chunks, by 7 in your case. For this, let's use this approach. Then, use a list comprehension producing a list of dictionaries:
>>> from pprint import pprint
>>> l = [['String 1'],['String 2'],['String 3'],['String 4'],['String 5'],
... ['String 6'],['String 7'],['String 8'],['String 9'],['String 10'],
... ['String 11']]
>>> def chunks(l, n):
... """Yield successive n-sized chunks from l."""
... for i in range(0, len(l), n):
... yield l[i:i+n]
...
>>>
>>> result = [{"title%d" % (i+1): chunk[i][0] for i in range(len(chunk))}
for chunk in chunks(l, 7)]
>>> pprint(result)
[{'title1': 'String 1',
'title2': 'String 2',
'title3': 'String 3',
'title4': 'String 4',
'title5': 'String 5',
'title6': 'String 6',
'title7': 'String 7'},
{'title1': 'String 8',
'title2': 'String 9',
'title3': 'String 10',
'title4': 'String 11'}]
Upvotes: 4