importing multiline JSON file in python












0















I am trying to import a JSON file in python that has many objects like the following:



{"ID": 1989,  "Attrib1": "74574d4c6",    "Attrib2": null,    "Attrib3": "41324" }
{"ID": 1990, "Attrib1": "1652857c6", "Attrib2": asd123, "Attrib3": "424" }


The file has line break for each object therefore, the json.load(file) is failing on the first line break.



I tried to iterate through them with:



with open(myFileLocation,'r') as myfile:
for line in myfile:
Data = json.loads(row)
return Data


but I couldn't add each line to a dictionary object as it doesn't have append (or any other method that am aware of).



How can I return all the objects in the JSON file as a dictionary?










share|improve this question

























  • You can create an empty dictionary with my_dict = {} and add new key-value pairs to it with my_dict[new_key] = new_value. First though, you'll need to decide which field of your JSON object you want as the key.

    – dmitriys
    Nov 25 '18 at 6:40


















0















I am trying to import a JSON file in python that has many objects like the following:



{"ID": 1989,  "Attrib1": "74574d4c6",    "Attrib2": null,    "Attrib3": "41324" }
{"ID": 1990, "Attrib1": "1652857c6", "Attrib2": asd123, "Attrib3": "424" }


The file has line break for each object therefore, the json.load(file) is failing on the first line break.



I tried to iterate through them with:



with open(myFileLocation,'r') as myfile:
for line in myfile:
Data = json.loads(row)
return Data


but I couldn't add each line to a dictionary object as it doesn't have append (or any other method that am aware of).



How can I return all the objects in the JSON file as a dictionary?










share|improve this question

























  • You can create an empty dictionary with my_dict = {} and add new key-value pairs to it with my_dict[new_key] = new_value. First though, you'll need to decide which field of your JSON object you want as the key.

    – dmitriys
    Nov 25 '18 at 6:40
















0












0








0








I am trying to import a JSON file in python that has many objects like the following:



{"ID": 1989,  "Attrib1": "74574d4c6",    "Attrib2": null,    "Attrib3": "41324" }
{"ID": 1990, "Attrib1": "1652857c6", "Attrib2": asd123, "Attrib3": "424" }


The file has line break for each object therefore, the json.load(file) is failing on the first line break.



I tried to iterate through them with:



with open(myFileLocation,'r') as myfile:
for line in myfile:
Data = json.loads(row)
return Data


but I couldn't add each line to a dictionary object as it doesn't have append (or any other method that am aware of).



How can I return all the objects in the JSON file as a dictionary?










share|improve this question
















I am trying to import a JSON file in python that has many objects like the following:



{"ID": 1989,  "Attrib1": "74574d4c6",    "Attrib2": null,    "Attrib3": "41324" }
{"ID": 1990, "Attrib1": "1652857c6", "Attrib2": asd123, "Attrib3": "424" }


The file has line break for each object therefore, the json.load(file) is failing on the first line break.



I tried to iterate through them with:



with open(myFileLocation,'r') as myfile:
for line in myfile:
Data = json.loads(row)
return Data


but I couldn't add each line to a dictionary object as it doesn't have append (or any other method that am aware of).



How can I return all the objects in the JSON file as a dictionary?







python json import






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 6:40









MaJoR

534115




534115










asked Nov 25 '18 at 6:29









MahmoudMahmoud

83




83













  • You can create an empty dictionary with my_dict = {} and add new key-value pairs to it with my_dict[new_key] = new_value. First though, you'll need to decide which field of your JSON object you want as the key.

    – dmitriys
    Nov 25 '18 at 6:40





















  • You can create an empty dictionary with my_dict = {} and add new key-value pairs to it with my_dict[new_key] = new_value. First though, you'll need to decide which field of your JSON object you want as the key.

    – dmitriys
    Nov 25 '18 at 6:40



















You can create an empty dictionary with my_dict = {} and add new key-value pairs to it with my_dict[new_key] = new_value. First though, you'll need to decide which field of your JSON object you want as the key.

– dmitriys
Nov 25 '18 at 6:40







You can create an empty dictionary with my_dict = {} and add new key-value pairs to it with my_dict[new_key] = new_value. First though, you'll need to decide which field of your JSON object you want as the key.

– dmitriys
Nov 25 '18 at 6:40














1 Answer
1






active

oldest

votes


















0














json.loads (and json.load) does not decode multiple json objects.



>>>json.loads('{}')
{}
>>>json.loads('{}{}')
JSONDecodeError Traceback (most
recent call last)
<ipython-input-7-0285b42a3a05> in <module>()
----> 1 json.loads('{}{}')

~AppDataLocalContinuumanaconda3libjson__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook
is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder

~AppDataLocalContinuumanaconda3libjsondecoder.py in decode(self, s, _w)
340 end = _w(s, end).end()
341 if end != len(s):
--> 342 raise JSONDecodeError("Extra data", s, end)
343 return obj
344

JSONDecodeError: Extra data: line 1 column 3 (char 2)


To dump multiple dictionaries, you could wrap them in a list by dumping the list.



>>> dict1 = {}
>>> dict2 = {}
>>> json.dumps([dict1, dict2])
'[{}, {}]'
>>> json.loads(json.dumps([dict1, dict2]))
[{}, {}]


Also Use append as follows



list = 
for line in open('data.json'), 'r'):
list.append(json.loads(line)





share|improve this answer


























  • I am able to add the file content as a list but, I need to analyze the JSON data and I can't do that on a list as far as I know it should be a dictionary.

    – Mahmoud
    Nov 25 '18 at 16:35











  • d = dict(itertools.zip_longest(l[::2], l[1::2], fillvalue='')) This can convert the list to a dictionary.

    – ersh
    Jan 22 at 7:13













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%2f53465203%2fimporting-multiline-json-file-in-python%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









0














json.loads (and json.load) does not decode multiple json objects.



>>>json.loads('{}')
{}
>>>json.loads('{}{}')
JSONDecodeError Traceback (most
recent call last)
<ipython-input-7-0285b42a3a05> in <module>()
----> 1 json.loads('{}{}')

~AppDataLocalContinuumanaconda3libjson__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook
is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder

~AppDataLocalContinuumanaconda3libjsondecoder.py in decode(self, s, _w)
340 end = _w(s, end).end()
341 if end != len(s):
--> 342 raise JSONDecodeError("Extra data", s, end)
343 return obj
344

JSONDecodeError: Extra data: line 1 column 3 (char 2)


To dump multiple dictionaries, you could wrap them in a list by dumping the list.



>>> dict1 = {}
>>> dict2 = {}
>>> json.dumps([dict1, dict2])
'[{}, {}]'
>>> json.loads(json.dumps([dict1, dict2]))
[{}, {}]


Also Use append as follows



list = 
for line in open('data.json'), 'r'):
list.append(json.loads(line)





share|improve this answer


























  • I am able to add the file content as a list but, I need to analyze the JSON data and I can't do that on a list as far as I know it should be a dictionary.

    – Mahmoud
    Nov 25 '18 at 16:35











  • d = dict(itertools.zip_longest(l[::2], l[1::2], fillvalue='')) This can convert the list to a dictionary.

    – ersh
    Jan 22 at 7:13


















0














json.loads (and json.load) does not decode multiple json objects.



>>>json.loads('{}')
{}
>>>json.loads('{}{}')
JSONDecodeError Traceback (most
recent call last)
<ipython-input-7-0285b42a3a05> in <module>()
----> 1 json.loads('{}{}')

~AppDataLocalContinuumanaconda3libjson__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook
is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder

~AppDataLocalContinuumanaconda3libjsondecoder.py in decode(self, s, _w)
340 end = _w(s, end).end()
341 if end != len(s):
--> 342 raise JSONDecodeError("Extra data", s, end)
343 return obj
344

JSONDecodeError: Extra data: line 1 column 3 (char 2)


To dump multiple dictionaries, you could wrap them in a list by dumping the list.



>>> dict1 = {}
>>> dict2 = {}
>>> json.dumps([dict1, dict2])
'[{}, {}]'
>>> json.loads(json.dumps([dict1, dict2]))
[{}, {}]


Also Use append as follows



list = 
for line in open('data.json'), 'r'):
list.append(json.loads(line)





share|improve this answer


























  • I am able to add the file content as a list but, I need to analyze the JSON data and I can't do that on a list as far as I know it should be a dictionary.

    – Mahmoud
    Nov 25 '18 at 16:35











  • d = dict(itertools.zip_longest(l[::2], l[1::2], fillvalue='')) This can convert the list to a dictionary.

    – ersh
    Jan 22 at 7:13
















0












0








0







json.loads (and json.load) does not decode multiple json objects.



>>>json.loads('{}')
{}
>>>json.loads('{}{}')
JSONDecodeError Traceback (most
recent call last)
<ipython-input-7-0285b42a3a05> in <module>()
----> 1 json.loads('{}{}')

~AppDataLocalContinuumanaconda3libjson__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook
is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder

~AppDataLocalContinuumanaconda3libjsondecoder.py in decode(self, s, _w)
340 end = _w(s, end).end()
341 if end != len(s):
--> 342 raise JSONDecodeError("Extra data", s, end)
343 return obj
344

JSONDecodeError: Extra data: line 1 column 3 (char 2)


To dump multiple dictionaries, you could wrap them in a list by dumping the list.



>>> dict1 = {}
>>> dict2 = {}
>>> json.dumps([dict1, dict2])
'[{}, {}]'
>>> json.loads(json.dumps([dict1, dict2]))
[{}, {}]


Also Use append as follows



list = 
for line in open('data.json'), 'r'):
list.append(json.loads(line)





share|improve this answer















json.loads (and json.load) does not decode multiple json objects.



>>>json.loads('{}')
{}
>>>json.loads('{}{}')
JSONDecodeError Traceback (most
recent call last)
<ipython-input-7-0285b42a3a05> in <module>()
----> 1 json.loads('{}{}')

~AppDataLocalContinuumanaconda3libjson__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook
is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder

~AppDataLocalContinuumanaconda3libjsondecoder.py in decode(self, s, _w)
340 end = _w(s, end).end()
341 if end != len(s):
--> 342 raise JSONDecodeError("Extra data", s, end)
343 return obj
344

JSONDecodeError: Extra data: line 1 column 3 (char 2)


To dump multiple dictionaries, you could wrap them in a list by dumping the list.



>>> dict1 = {}
>>> dict2 = {}
>>> json.dumps([dict1, dict2])
'[{}, {}]'
>>> json.loads(json.dumps([dict1, dict2]))
[{}, {}]


Also Use append as follows



list = 
for line in open('data.json'), 'r'):
list.append(json.loads(line)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 6:48

























answered Nov 25 '18 at 6:43









ershersh

243




243













  • I am able to add the file content as a list but, I need to analyze the JSON data and I can't do that on a list as far as I know it should be a dictionary.

    – Mahmoud
    Nov 25 '18 at 16:35











  • d = dict(itertools.zip_longest(l[::2], l[1::2], fillvalue='')) This can convert the list to a dictionary.

    – ersh
    Jan 22 at 7:13





















  • I am able to add the file content as a list but, I need to analyze the JSON data and I can't do that on a list as far as I know it should be a dictionary.

    – Mahmoud
    Nov 25 '18 at 16:35











  • d = dict(itertools.zip_longest(l[::2], l[1::2], fillvalue='')) This can convert the list to a dictionary.

    – ersh
    Jan 22 at 7:13



















I am able to add the file content as a list but, I need to analyze the JSON data and I can't do that on a list as far as I know it should be a dictionary.

– Mahmoud
Nov 25 '18 at 16:35





I am able to add the file content as a list but, I need to analyze the JSON data and I can't do that on a list as far as I know it should be a dictionary.

– Mahmoud
Nov 25 '18 at 16:35













d = dict(itertools.zip_longest(l[::2], l[1::2], fillvalue='')) This can convert the list to a dictionary.

– ersh
Jan 22 at 7:13







d = dict(itertools.zip_longest(l[::2], l[1::2], fillvalue='')) This can convert the list to a dictionary.

– ersh
Jan 22 at 7:13






















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%2f53465203%2fimporting-multiline-json-file-in-python%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

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga