Convert Json to newline Json standard using Python

前端 未结 2 1437
慢半拍i
慢半拍i 2021-01-27 13:27

I have a code which get nested object and remove all nesting (make the object flat):

def flatten_json(y):
    \"\"\"
    @param y: Unflated Json
    @return: Fla         


        
2条回答
  •  时光说笑
    2021-01-27 13:57

    I think you're looking for something like this?

    import json
    
    def create_jsonlines(original):
    
        if isinstance(original, str):
            original = json.loads(original)
    
        return '\n'.join([json.dumps(original[outer_key], sort_keys=True) 
                          for outer_key in sorted(original.keys(),
                                                  key=lambda x: int(x))])
    
    # Added fake record to prove order is sorted
    inp = {
       "3": {"code": "en-FR", "id": 76, "name": "French", "languageName": "French"},
       "0": {"code": "en-GB", "id": 77, "languageName": "English", "name": "English"}, 
       "1": {"code": "de-DE", "id": 78, "languageName": "Deutsch", "name": "German"}
       }
    output = create_jsonlines(inp)
    
    print(output)
    

提交回复
热议问题