-4

{"i": "56", "m1": "IT", "m2": "Area", "r": "faced this problem"}

{"i": "57", "m1": "IT", "m2": "Area", "r": "faced this problem3"}

this is the format of JSON?

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
Preeti
  • 1
  • 1
    Show what you have tried so its easy to solve your problem you should try 1.read json and loop on json and save data to csv file using csv.write() – ravishankar chavare Mar 05 '19 at 09:52
  • go through this [MCVE](https://stackoverflow.com/help/mcve) .TO CHECK JSON VALIDATOR go through this [Json validator](https://jsonlint.com/) – sahasrara62 Mar 05 '19 at 09:53
  • Please read through the answers here: [How can I convert JSON to CSV?](https://stackoverflow.com/q/1871524/9423231) Write some code yourself and if your code doesn't work post the code in your question. – frido Mar 05 '19 at 09:56

1 Answers1

0

Code

import json

with open('input.json', "r") as in_file:
    data = json.load(in_file)

with open('output.csv', 'w') as out_file:
    for entry in data:
        out_file.write(','.join(entry.values()) + '\n')

input.json

[
  {
    "i": "56",
    "m1": "IT",
    "m2": "Area",
    "r": "faced this problem"
  },
  {
    "i": "57",
    "m1": "IT",
    "m2": "Area",
    "r": "faced this problem3"
  }
]

output.csv

56,IT,Area,faced this problem
57,IT,Area,faced this problem3
balderman
  • 22,927
  • 7
  • 34
  • 52
  • These data is in json file, same way having lot of data. My code only writing one line and giving encoding error. – Preeti Mar 05 '19 at 14:08
  • Please explain. Be as detailed as you can. – balderman Mar 05 '19 at 14:14
  • Have a json file the format of the file {"i": "56", "m1": "IT", "m2": "Area", "r": "faced this problem"} , this is one line. Like wise many lines are there in that file. This data needs to transferred to csv file. Using python 3.x – Preeti Mar 05 '19 at 14:21
  • Do you know how to load JSON file into memory as a python dict? If you dont know - a quick search in the web should help. Once you are done with the JSON loading part you will have the 'DATA' (see my code) and you can go on. – balderman Mar 05 '19 at 14:35
  • import json with open('inputfilePath', "r") as data_file: for line in data_file.readlines(): data = json.loads(line) print(data) data_file.close() with open('outputfile', 'w') as f: for entry in data: f.write(','.join(entry.values()) + '\n') – Preeti Mar 05 '19 at 17:10
  • see above code , I am doing that but getting error as , f.write(','.join(entry.values()) + '\n') AttributeError: 'str' object has no attribute 'values' – Preeti Mar 05 '19 at 17:11
  • Add the code above to the question body (as code) under the title "This is how I load the JSON into dict". Are you able to do it? Do toy get the dict? – balderman Mar 05 '19 at 17:12
  • Share the input file as well (or a subset of it) – balderman Mar 05 '19 at 17:13
  • Instead of reading from fixed value I am reading it from a input file and the input file format is a dict format. one line is one json in that input json file. – Preeti Mar 05 '19 at 17:18
  • Share the input file please. (Code was modified with detailed example) – balderman Mar 05 '19 at 17:19
  • Input file name : TestInput.json {"i": "56", "m1": "IT", "m3": "Area", "r": "faced this problem13"} {"i": "57", "m1": "IT", "m5": "Area", "r": "faced this problem33"} {"i": "58", "m1": "IT", "m28": "Area", "r": "faced this problem55"} {"i": "60", "m1": "IT", "m26: "Area", "r": "faced this problem3"} – Preeti Mar 05 '19 at 17:23
  • Add square brackets around the data: [ {"i": "56", "m1": "IT", "m3": "Area", "r": "faced this problem13"} {"i": "57", "m1": "IT", "m5": "Area", "r": "faced this problem33"} {"i": "58", "m1": "IT", "m28": "Area", "r": "faced this problem55"} {"i": "60", "m1": "IT", "m26: "Area", "r": "faced this problem3"} ] in the file. **See the example above** – balderman Mar 05 '19 at 17:26
  • after adding that getting this error : data = json.loads(line) File "/usr/lib/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/usr/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.6/json/decoder.py", line 355, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ',' delimiter: line 2 column 1 (char 1405) – Preeti Mar 05 '19 at 17:30
  • Use https://pastebin.com/ and share a **formatted** and **valid** sample JSON. Use http://jsonviewer.stack.hu/ in order to check that your JSON is valid – balderman Mar 05 '19 at 17:30