skip to Main Content

In Python, I would like to get all elements from my lists BIRD_answer and my_answer in a single row:

{
    "BIRD_answer":[
        [
            0.043478260869565216
        ],
        [
            0.07042253521126761
        ],
        [
            0.11363636363636363
        ]
    ],
    "my_answer":[
        [
            "MillenniumHighAlternative",
            0.043478260869565216
        ],
        [
            "RanchodelMarHigh(Continuation)",
            0.07042253521126761
        ],
        [
            "DelAmigoHigh(Continuation)",
            0.11363636363636363
        ]
    ]
}

To give me a format like this :

    "BIRD_answer":[[0.043478260869565216],[0.07042253521126761],[0.11363636363636363]],
    "my_answer":[["MillenniumHighAlternative",0.043478260869565216],["RanchodelMarHigh(Continuation)",0.07042253521126761],["DelAmigoHigh(Continuation)",0.11363636363636363]]
}

I have already tried to use jsbeautifier but doesn’t work.

2

Answers


  1. You can do it using json.dumps

    import json
    
    data = {
        "BIRD_answer": [
            [0.043478260869565216],
            [0.07042253521126761],
            [0.11363636363636363]
        ],
        "my_answer": [
            ["MillenniumHighAlternative", 0.043478260869565216],
            ["RanchodelMarHigh(Continuation)", 0.07042253521126761],
            ["DelAmigoHigh(Continuation)", 0.11363636363636363]
        ]
    }
    
    formatted_lines = []
    
    for key, value in data.items():
        compact_value = json.dumps({key: value}, separators=(',', ':'))
        formatted_lines.append(f"    {compact_value[1:-1]}")
    
    formatted_json = "{n" + ",n".join(formatted_lines) + "n}"
    
    print(formatted_json)
    

    Result :

    {
        "BIRD_answer":[[0.043478260869565216],[0.07042253521126761],[0.11363636363636363]],
        "my_answer":[["MillenniumHighAlternative",0.043478260869565216],["RanchodelMarHigh(Continuation)",0.07042253521126761],["DelAmigoHigh(Continuation)",0.11363636363636363]]
    }
    

    Hope it helps you!

    result-image

    Login or Signup to reply.
  2. You will need to devise a custom formatter:

    #!/usr/bin/env python3
    
    import json
    
    json_data = '''
    {
      "BIRD_answer": [
        [0.043478260869565216],
        [0.07042253521126761],
        [0.11363636363636363]
      ],
      "my_answer": [
        ["MillenniumHighAlternative", 0.043478260869565216],
        ["RanchodelMarHigh(Continuation)", 0.07042253521126761],
        ["DelAmigoHigh(Continuation)", 0.11363636363636363]
      ]
    }
    '''
    
    def format_json(json_str: str, indent_char: str = ' ', indent_size: int = 4) -> str:
        parsed_data = json.loads(json_str)
        indent = indent_char * indent_size
        formatted_json = "{n"
        for key, value in parsed_data.items():
            minified_value = json.dumps(value, separators=(',', ':'))
            formatted_json += f'{indent}"{key}": {minified_value},n'
        return formatted_json.rstrip(',n') + 'n}'
    
    if __name__ == '__main__':
        print(format_json(json_data))
    

    This will result in:

    {
        "BIRD_answer": [[0.043478260869565216],[0.07042253521126761],[0.11363636363636363]],
        "my_answer": [["MillenniumHighAlternative",0.043478260869565216],["RanchodelMarHigh(Continuation)",0.07042253521126761],["DelAmigoHigh(Continuation)",0.11363636363636363]]
    }
    

    Here is a more compact function:

    def format_json(json_str: str, indent_char: str = ' ', indent_size: int = 4) -> str:
        parsed_data, indent = json.loads(json_str), indent_char * indent_size
        
        return "{n" + "n".join(
            f'{indent}"{key}": {json.dumps(value, separators=(",", ":"))},'
            for key, value in parsed_data.items()
        ).rstrip(',') + "n}"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search