I need to know how to implement to_json_string_list()
function in that case:
JavaScript
x
6
1
df = pandas.read_json('[{"rec1" : "val1", "rec2" : "val4"}, {"rec1" : "val3", "rec2" : "val4"}]', orient='records')
2
json_strings = list()
3
json_strings = to_json_string_list(df)
4
for json_string in json_strings:
5
print(json_string)
6
to get output like:
{“rec1” : “val1”, “rec2” : “val4”}
{“rec1” : “val3”, “rec2” : “val4”}
I know that there are function to_json(orient='records')
, but it is not that I need, because I get:
[{“rec1” : “val1”, “rec2” : “val4”},
{“rec1” : “val3”, “rec2” : “val4”}]
Printing is not only thing I will do with this strings, so simple substitution of [],
is not what I need, too.
Advertisement
Answer
I think you need to_json
with parameter lines=True
:
JavaScript
1
6
1
print (df.to_json(orient='records', lines=True))
2
{"rec1":"val1","rec2":"val4"}
3
{"rec1":"val3","rec2":"val4"}
4
5
df.to_json('file.json',orient='records', lines=True)
6