How to parse a JSON string with no root element?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a JSON file like below:



{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}


So basically the file contains a list of people.



I tried to use json.loads(str_content) in Python 3, but it returned the error of json.decoder.JSONDecodeError: Extra data:.



I checked with an online JSON parser (http://json.parser.online.fr) and it told me the same problem.



How to parse a JSON file without a root element but a list of JSON objects?










share|improve this question

























  • There is a syntax error in age:20

    – Srce Cde
    Nov 22 '18 at 8:40








  • 4





    The problem is you don't have a JSON file; what's creating that file, could it be modified to have a single root array?

    – jonrsharpe
    Nov 22 '18 at 8:40











  • @Chirag thanks, I modified it.

    – mommomonthewind
    Nov 22 '18 at 8:42











  • @jonrsharpe I think the file is created from MongoDB. In fact I god a BSON file, then I converted it to JSON.

    – mommomonthewind
    Nov 22 '18 at 8:43











  • Is it stored in multi-line json? Like this {"name":"A","age":19} {"name":"B","age":20} Each in new line

    – Srce Cde
    Nov 22 '18 at 8:45




















0















I have a JSON file like below:



{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}


So basically the file contains a list of people.



I tried to use json.loads(str_content) in Python 3, but it returned the error of json.decoder.JSONDecodeError: Extra data:.



I checked with an online JSON parser (http://json.parser.online.fr) and it told me the same problem.



How to parse a JSON file without a root element but a list of JSON objects?










share|improve this question

























  • There is a syntax error in age:20

    – Srce Cde
    Nov 22 '18 at 8:40








  • 4





    The problem is you don't have a JSON file; what's creating that file, could it be modified to have a single root array?

    – jonrsharpe
    Nov 22 '18 at 8:40











  • @Chirag thanks, I modified it.

    – mommomonthewind
    Nov 22 '18 at 8:42











  • @jonrsharpe I think the file is created from MongoDB. In fact I god a BSON file, then I converted it to JSON.

    – mommomonthewind
    Nov 22 '18 at 8:43











  • Is it stored in multi-line json? Like this {"name":"A","age":19} {"name":"B","age":20} Each in new line

    – Srce Cde
    Nov 22 '18 at 8:45
















0












0








0








I have a JSON file like below:



{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}


So basically the file contains a list of people.



I tried to use json.loads(str_content) in Python 3, but it returned the error of json.decoder.JSONDecodeError: Extra data:.



I checked with an online JSON parser (http://json.parser.online.fr) and it told me the same problem.



How to parse a JSON file without a root element but a list of JSON objects?










share|improve this question
















I have a JSON file like below:



{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}


So basically the file contains a list of people.



I tried to use json.loads(str_content) in Python 3, but it returned the error of json.decoder.JSONDecodeError: Extra data:.



I checked with an online JSON parser (http://json.parser.online.fr) and it told me the same problem.



How to parse a JSON file without a root element but a list of JSON objects?







python json python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 8:42







mommomonthewind

















asked Nov 22 '18 at 8:38









mommomonthewindmommomonthewind

75441328




75441328













  • There is a syntax error in age:20

    – Srce Cde
    Nov 22 '18 at 8:40








  • 4





    The problem is you don't have a JSON file; what's creating that file, could it be modified to have a single root array?

    – jonrsharpe
    Nov 22 '18 at 8:40











  • @Chirag thanks, I modified it.

    – mommomonthewind
    Nov 22 '18 at 8:42











  • @jonrsharpe I think the file is created from MongoDB. In fact I god a BSON file, then I converted it to JSON.

    – mommomonthewind
    Nov 22 '18 at 8:43











  • Is it stored in multi-line json? Like this {"name":"A","age":19} {"name":"B","age":20} Each in new line

    – Srce Cde
    Nov 22 '18 at 8:45





















  • There is a syntax error in age:20

    – Srce Cde
    Nov 22 '18 at 8:40








  • 4





    The problem is you don't have a JSON file; what's creating that file, could it be modified to have a single root array?

    – jonrsharpe
    Nov 22 '18 at 8:40











  • @Chirag thanks, I modified it.

    – mommomonthewind
    Nov 22 '18 at 8:42











  • @jonrsharpe I think the file is created from MongoDB. In fact I god a BSON file, then I converted it to JSON.

    – mommomonthewind
    Nov 22 '18 at 8:43











  • Is it stored in multi-line json? Like this {"name":"A","age":19} {"name":"B","age":20} Each in new line

    – Srce Cde
    Nov 22 '18 at 8:45



















There is a syntax error in age:20

– Srce Cde
Nov 22 '18 at 8:40







There is a syntax error in age:20

– Srce Cde
Nov 22 '18 at 8:40






4




4





The problem is you don't have a JSON file; what's creating that file, could it be modified to have a single root array?

– jonrsharpe
Nov 22 '18 at 8:40





The problem is you don't have a JSON file; what's creating that file, could it be modified to have a single root array?

– jonrsharpe
Nov 22 '18 at 8:40













@Chirag thanks, I modified it.

– mommomonthewind
Nov 22 '18 at 8:42





@Chirag thanks, I modified it.

– mommomonthewind
Nov 22 '18 at 8:42













@jonrsharpe I think the file is created from MongoDB. In fact I god a BSON file, then I converted it to JSON.

– mommomonthewind
Nov 22 '18 at 8:43





@jonrsharpe I think the file is created from MongoDB. In fact I god a BSON file, then I converted it to JSON.

– mommomonthewind
Nov 22 '18 at 8:43













Is it stored in multi-line json? Like this {"name":"A","age":19} {"name":"B","age":20} Each in new line

– Srce Cde
Nov 22 '18 at 8:45







Is it stored in multi-line json? Like this {"name":"A","age":19} {"name":"B","age":20} Each in new line

– Srce Cde
Nov 22 '18 at 8:45














1 Answer
1






active

oldest

votes


















3














The issue is that the string you are trying to parse is not a valid JSON document. It is actually a concatenation of JSON documents. So the simple json.loads() will not work.



You can use instead something based on https://docs.python.org/3/library/json.html#json.JSONDecoder.raw_decode . E.g: (code is a bit ugly but the logic should be clear):



import json

s = """{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}"""

def iter_jsons(s):
decoder = json.JSONDecoder()

i = 0
while True:
doc, i2 = decoder.raw_decode(s[i:].strip())
yield doc
if i == i2:
break
i= i2

print(list(iter_jsons(s)))





[{'name': 'A', 'age': 19}, {'name': 'B', 'age': 20}]









share|improve this answer


























  • If there are no nested dicts you could simply look for the closing brace and parse up to that point.

    – tripleee
    Nov 22 '18 at 9:00











  • I can't find anything in the json standard that indicates that a JSON document must have a root element.

    – Tomas Zubiri
    Nov 22 '18 at 9:08











  • @TomasZubiri It is not explicitly stated as text in tools.ietf.org/html/rfc7159#section-2 , but the ABNF indicates that a JSON-text is a SINGLE value

    – Guillaume
    Nov 22 '18 at 10:08








  • 1





    @TomasZubiri: I believe the standard indicates that a JSON document must either be a single JSON object enclosed in {, } brackets or a comma delimited list of them enclosed in [, ] brackets. This answer looks like a very clever workaround allowing this non-compliant input to be decoded into the list it should have been in the first place.

    – martineau
    Nov 22 '18 at 10:11













  • @tripleee: that is correct, but I'd rather not assume anything about the content I am trying to parse.

    – Guillaume
    Nov 22 '18 at 10:15












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426839%2fhow-to-parse-a-json-string-with-no-root-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














The issue is that the string you are trying to parse is not a valid JSON document. It is actually a concatenation of JSON documents. So the simple json.loads() will not work.



You can use instead something based on https://docs.python.org/3/library/json.html#json.JSONDecoder.raw_decode . E.g: (code is a bit ugly but the logic should be clear):



import json

s = """{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}"""

def iter_jsons(s):
decoder = json.JSONDecoder()

i = 0
while True:
doc, i2 = decoder.raw_decode(s[i:].strip())
yield doc
if i == i2:
break
i= i2

print(list(iter_jsons(s)))





[{'name': 'A', 'age': 19}, {'name': 'B', 'age': 20}]









share|improve this answer


























  • If there are no nested dicts you could simply look for the closing brace and parse up to that point.

    – tripleee
    Nov 22 '18 at 9:00











  • I can't find anything in the json standard that indicates that a JSON document must have a root element.

    – Tomas Zubiri
    Nov 22 '18 at 9:08











  • @TomasZubiri It is not explicitly stated as text in tools.ietf.org/html/rfc7159#section-2 , but the ABNF indicates that a JSON-text is a SINGLE value

    – Guillaume
    Nov 22 '18 at 10:08








  • 1





    @TomasZubiri: I believe the standard indicates that a JSON document must either be a single JSON object enclosed in {, } brackets or a comma delimited list of them enclosed in [, ] brackets. This answer looks like a very clever workaround allowing this non-compliant input to be decoded into the list it should have been in the first place.

    – martineau
    Nov 22 '18 at 10:11













  • @tripleee: that is correct, but I'd rather not assume anything about the content I am trying to parse.

    – Guillaume
    Nov 22 '18 at 10:15
















3














The issue is that the string you are trying to parse is not a valid JSON document. It is actually a concatenation of JSON documents. So the simple json.loads() will not work.



You can use instead something based on https://docs.python.org/3/library/json.html#json.JSONDecoder.raw_decode . E.g: (code is a bit ugly but the logic should be clear):



import json

s = """{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}"""

def iter_jsons(s):
decoder = json.JSONDecoder()

i = 0
while True:
doc, i2 = decoder.raw_decode(s[i:].strip())
yield doc
if i == i2:
break
i= i2

print(list(iter_jsons(s)))





[{'name': 'A', 'age': 19}, {'name': 'B', 'age': 20}]









share|improve this answer


























  • If there are no nested dicts you could simply look for the closing brace and parse up to that point.

    – tripleee
    Nov 22 '18 at 9:00











  • I can't find anything in the json standard that indicates that a JSON document must have a root element.

    – Tomas Zubiri
    Nov 22 '18 at 9:08











  • @TomasZubiri It is not explicitly stated as text in tools.ietf.org/html/rfc7159#section-2 , but the ABNF indicates that a JSON-text is a SINGLE value

    – Guillaume
    Nov 22 '18 at 10:08








  • 1





    @TomasZubiri: I believe the standard indicates that a JSON document must either be a single JSON object enclosed in {, } brackets or a comma delimited list of them enclosed in [, ] brackets. This answer looks like a very clever workaround allowing this non-compliant input to be decoded into the list it should have been in the first place.

    – martineau
    Nov 22 '18 at 10:11













  • @tripleee: that is correct, but I'd rather not assume anything about the content I am trying to parse.

    – Guillaume
    Nov 22 '18 at 10:15














3












3








3







The issue is that the string you are trying to parse is not a valid JSON document. It is actually a concatenation of JSON documents. So the simple json.loads() will not work.



You can use instead something based on https://docs.python.org/3/library/json.html#json.JSONDecoder.raw_decode . E.g: (code is a bit ugly but the logic should be clear):



import json

s = """{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}"""

def iter_jsons(s):
decoder = json.JSONDecoder()

i = 0
while True:
doc, i2 = decoder.raw_decode(s[i:].strip())
yield doc
if i == i2:
break
i= i2

print(list(iter_jsons(s)))





[{'name': 'A', 'age': 19}, {'name': 'B', 'age': 20}]









share|improve this answer















The issue is that the string you are trying to parse is not a valid JSON document. It is actually a concatenation of JSON documents. So the simple json.loads() will not work.



You can use instead something based on https://docs.python.org/3/library/json.html#json.JSONDecoder.raw_decode . E.g: (code is a bit ugly but the logic should be clear):



import json

s = """{
"name":"A",
"age":19
}
{
"name":"B",
"age":20
}"""

def iter_jsons(s):
decoder = json.JSONDecoder()

i = 0
while True:
doc, i2 = decoder.raw_decode(s[i:].strip())
yield doc
if i == i2:
break
i= i2

print(list(iter_jsons(s)))





[{'name': 'A', 'age': 19}, {'name': 'B', 'age': 20}]










share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 10:14

























answered Nov 22 '18 at 8:54









GuillaumeGuillaume

3,1081830




3,1081830













  • If there are no nested dicts you could simply look for the closing brace and parse up to that point.

    – tripleee
    Nov 22 '18 at 9:00











  • I can't find anything in the json standard that indicates that a JSON document must have a root element.

    – Tomas Zubiri
    Nov 22 '18 at 9:08











  • @TomasZubiri It is not explicitly stated as text in tools.ietf.org/html/rfc7159#section-2 , but the ABNF indicates that a JSON-text is a SINGLE value

    – Guillaume
    Nov 22 '18 at 10:08








  • 1





    @TomasZubiri: I believe the standard indicates that a JSON document must either be a single JSON object enclosed in {, } brackets or a comma delimited list of them enclosed in [, ] brackets. This answer looks like a very clever workaround allowing this non-compliant input to be decoded into the list it should have been in the first place.

    – martineau
    Nov 22 '18 at 10:11













  • @tripleee: that is correct, but I'd rather not assume anything about the content I am trying to parse.

    – Guillaume
    Nov 22 '18 at 10:15



















  • If there are no nested dicts you could simply look for the closing brace and parse up to that point.

    – tripleee
    Nov 22 '18 at 9:00











  • I can't find anything in the json standard that indicates that a JSON document must have a root element.

    – Tomas Zubiri
    Nov 22 '18 at 9:08











  • @TomasZubiri It is not explicitly stated as text in tools.ietf.org/html/rfc7159#section-2 , but the ABNF indicates that a JSON-text is a SINGLE value

    – Guillaume
    Nov 22 '18 at 10:08








  • 1





    @TomasZubiri: I believe the standard indicates that a JSON document must either be a single JSON object enclosed in {, } brackets or a comma delimited list of them enclosed in [, ] brackets. This answer looks like a very clever workaround allowing this non-compliant input to be decoded into the list it should have been in the first place.

    – martineau
    Nov 22 '18 at 10:11













  • @tripleee: that is correct, but I'd rather not assume anything about the content I am trying to parse.

    – Guillaume
    Nov 22 '18 at 10:15

















If there are no nested dicts you could simply look for the closing brace and parse up to that point.

– tripleee
Nov 22 '18 at 9:00





If there are no nested dicts you could simply look for the closing brace and parse up to that point.

– tripleee
Nov 22 '18 at 9:00













I can't find anything in the json standard that indicates that a JSON document must have a root element.

– Tomas Zubiri
Nov 22 '18 at 9:08





I can't find anything in the json standard that indicates that a JSON document must have a root element.

– Tomas Zubiri
Nov 22 '18 at 9:08













@TomasZubiri It is not explicitly stated as text in tools.ietf.org/html/rfc7159#section-2 , but the ABNF indicates that a JSON-text is a SINGLE value

– Guillaume
Nov 22 '18 at 10:08







@TomasZubiri It is not explicitly stated as text in tools.ietf.org/html/rfc7159#section-2 , but the ABNF indicates that a JSON-text is a SINGLE value

– Guillaume
Nov 22 '18 at 10:08






1




1





@TomasZubiri: I believe the standard indicates that a JSON document must either be a single JSON object enclosed in {, } brackets or a comma delimited list of them enclosed in [, ] brackets. This answer looks like a very clever workaround allowing this non-compliant input to be decoded into the list it should have been in the first place.

– martineau
Nov 22 '18 at 10:11







@TomasZubiri: I believe the standard indicates that a JSON document must either be a single JSON object enclosed in {, } brackets or a comma delimited list of them enclosed in [, ] brackets. This answer looks like a very clever workaround allowing this non-compliant input to be decoded into the list it should have been in the first place.

– martineau
Nov 22 '18 at 10:11















@tripleee: that is correct, but I'd rather not assume anything about the content I am trying to parse.

– Guillaume
Nov 22 '18 at 10:15





@tripleee: that is correct, but I'd rather not assume anything about the content I am trying to parse.

– Guillaume
Nov 22 '18 at 10:15




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426839%2fhow-to-parse-a-json-string-with-no-root-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?