0

I have an excel sheet with two columns and I want to convert it into json. enter image description here

I want to convert this into a json format like this:

{"A": "This is a Sentence", "B": "1"},
{"A": "This is a Sentence", "B": "2"},
{"A": "This is a Sentence", "B": "3"},
{"A": "This is a Sentence", "B": "4"}

I tried using this command:

data= df.to_json (r'C:\Users\Admin\df_json.json',orient='split')

but it generates something like this:

[{"A": "This is a Sentence", "B": "1"},{"A": "This is a Sentence", "B": "2"},{"A": "This is a Sentence", "B": "3"},{"A": "This is a Sentence", "B": "4"},]
2
  • 2
    So that is same. Isn't it? Commented Aug 6, 2019 at 10:58
  • No, it is generated as a list,inside a square bracket. I don't want the square bracket. Commented Aug 6, 2019 at 11:00

1 Answer 1

2

The result you are getting is normal json. What you described as your desired result is json lines (also called jsonlines, jsonl or similar). There's no built-in support for it as far as I know, but if you want to output json lines it's pretty easy to do by hand. Here's an example on how you would write json lines to a file:

data = df.to_json (r'C:\Users\Admin\df_json.json', orient='split')
with open("converted.jsonl", mode="wt", encoding="utf-8") as f:
    for line in data:
        f.write(json.dumps(line) + "\n")
Sign up to request clarification or add additional context in comments.

1 Comment

this gives an error: "'NoneType' object is not iterable" at line 3 which means 'data' is of type None. Am I doing something wrong ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.