Saturday, May 18, 2024
HomeGolangFairly print JSON - Getting Assist

Fairly print JSON – Getting Assist


I’ve a bunch of JSON objects that I’d prefer to print to the display screen.
I’d prefer to type the keys to make them simpler to learn.
This python code does what I would like:

#!/usr/bin/env python3
import json

def reorder(s):
    o = json.masses(s)
    return json.dumps(o, sort_keys=True)

input1 = '{"b": 2, "A":1, "c": {"d": 3}}'
input2 = '{"c": {"d": 3}, "b": 2, "A":1}'
print(input1)
print(input2)
print()
print(reorder(input1))
print(reorder(input2))

Outputs:

{"b": 2, "A":1, "c": {"d": 3}}
{"c": {"d": 3}, "b": 2, "A":1}

{"A": 1, "b": 2, "c": {"d": 3}}
{"A": 1, "b": 2, "c": {"d": 3}}

I attempted utilizing json.Unmarshal and json.Marshal however it filters out lowercase keys.
It additionally leaves no areas within the output, which is a bit of laborious to learn.

I don’t know the format of the messages forward of time.
I solely know that they are going to be JSON objects.

Anybody have a great way of doing this?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments