Python - convert csv file to JSON

前端 未结 4 1388
醉梦人生
醉梦人生 2020-12-19 04:44

I need to convert a csv file into a hierarchical JSON object (preferably using Python). I thought that the script I have (below) does a correct job of converting to JSON, bu

4条回答
  •  被撕碎了的回忆
    2020-12-19 05:25

    Order of elements is not important for json.

    The json you are producing is wrong, though. The example you gave that you are trying to match does not have a "subject" key at the top level. It shows just "name" => "flare", and a list of children. Your script is producing json with a "subject" key.

    Instead of

    subject_dict = {'name':subject,'type':'subject','children':branch_list}
    

    try

    subject_dict = {'name':subject,'children':branch_list}
    

    I don't know about D3.js, so I can't say if it expects an object that follows one exact structure.

    Tip: one good tool to check the structure of json objects is http://jsoneditor.appspot.com. Just paste your string in there and click on "Go to Treeview". It will hellp you find differences between what you have and what you want very easily.

提交回复
热议问题