9

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.?

2

5 Answers 5

17

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})
4

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'}]
2
  • If I want multiple titles (e.g., "title", "convert", "json", ...), instead of title1, ttitle2, ... incumnting. Jun 6, 2016 at 16:28
  • 1
    @ahmadhas well, sorry, answering the question that was asked.
    – alecxe
    Jun 6, 2016 at 16:30
1

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'}]
0

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)
0
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'}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.