Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:24:52+00:00 2026-05-27T22:24:52+00:00

I have thousands of text files containing multiple JSON objects, but unfortunately there is

  • 0

I have thousands of text files containing multiple JSON objects, but unfortunately there is no delimiter between the objects.

The objects are stored as dictionaries and some of their fields are themselves objects. Each object might have a variable number of nested objects. Concretely, an object might look like this:

{field1: {}, field2: "some value", field3: {}, ...} 

and hundreds of such objects are concatenated without a delimiter in a text file. This means that I can neither use json.load() nor json.loads().

Any suggestion on how I can solve this problem. Is there a known parser to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:24:52+00:00Added an answer on May 27, 2026 at 10:24 pm

    This decodes your “list” of JSON Objects from a string:

    from json import JSONDecoder
    
    def loads_invalid_obj_list(s):
        decoder = JSONDecoder()
        s_len = len(s)
    
        objs = []
        end = 0
        while end != s_len:
            obj, end = decoder.raw_decode(s, idx=end)
            objs.append(obj)
    
        return objs
    

    The bonus here is that you play nice with the parser. Hence it keeps telling you exactly where it found an error.

    Examples

    >>> loads_invalid_obj_list('{}{}')
    [{}, {}]
    
    >>> loads_invalid_obj_list('{}{\n}{')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "decode.py", line 9, in loads_invalid_obj_list
        obj, end = decoder.raw_decode(s, idx=end)
      File     "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 376, in raw_decode
        obj, end = self.scan_once(s, idx)
    ValueError: Expecting object: line 2 column 2 (char 5)
    

    Clean Solution (added later)

    import json
    import re
    
    #shameless copy paste from json/decoder.py
    FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
    WHITESPACE = re.compile(r'[ \t\n\r]*', FLAGS)
    
    class ConcatJSONDecoder(json.JSONDecoder):
        def decode(self, s, _w=WHITESPACE.match):
            s_len = len(s)
    
            objs = []
            end = 0
            while end != s_len:
                obj, end = self.raw_decode(s, idx=_w(s, end).end())
                end = _w(s, end).end()
                objs.append(obj)
            return objs
    

    Examples

    >>> print json.loads('{}', cls=ConcatJSONDecoder)
    [{}]
    
    >>> print json.load(open('file'), cls=ConcatJSONDecoder)
    [{}]
    
    >>> print json.loads('{}{} {', cls=ConcatJSONDecoder)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
        return cls(encoding=encoding, **kw).decode(s)
      File "decode.py", line 15, in decode
        obj, end = self.raw_decode(s, idx=_w(s, end).end())
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 376, in raw_decode
        obj, end = self.scan_once(s, idx)
    ValueError: Expecting object: line 1 column 5 (char 5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently we have thousands of Microsoft Word files, Excel files, PDF's, images etc stored
I have a folder with thousands of text files and I'd like to paste
I'm trying to parse thousands of spec sheet text files containing company, material, chemical
I have written a java program which needs to process thousands of text files
I have thousands of text files, and each file contains just a single line
I have text files that have list of thousands of names like this DallasWebJobs
We receive multiple thousands of flat files per week currently, and I have a
We have a CMS which has several thousand text/html files in it. It turns
I have a text file that might contain thousands and thousands of numbers(0-9 -->
I have a simple text file with several thousands of words, each in its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.