importing multiline JSON file in python
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
add a comment |
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
You can create an empty dictionary withmy_dict = {}
and add new key-value pairs to it withmy_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
add a comment |
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
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
python json import
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 withmy_dict = {}
and add new key-value pairs to it withmy_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
add a comment |
You can create an empty dictionary withmy_dict = {}
and add new key-value pairs to it withmy_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
add a comment |
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You can create an empty dictionary with
my_dict = {}
and add new key-value pairs to it withmy_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