Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown to JSON processing badly handles empty values #23

Open
SuperMayo opened this issue Feb 14, 2019 · 1 comment
Open

Markdown to JSON processing badly handles empty values #23

SuperMayo opened this issue Feb 14, 2019 · 1 comment

Comments

@SuperMayo
Copy link

There is a possible improvement in b696398 in the way we convert markdown to JSON.

Bug:
If the value is empty (for example - identifier: ), the equivalent json will be

{
...
"identifier": "",
...
}

which is both ugly and nonstandard for JSON.
The convention would rather be to have no name/value pair at all or having the null value

Solution:
My improved code based on the actual one:

observations = []
record = dict()
for line in txt.split("\n")[2:]:
    if line[:2] == "##":
        print(line)
        if len(record) != 0:
            observations.append(record)
        record = dict()
    else:
        if not line.strip()=="":
            print(f'process "{line}"')
            lhs, rhs = (line[2:].split(":"))
            lhs = lhs.strip()
            rhs = rhs.strip()
            if rhs != ""
                record[lhs] = rhs
            else:
                record[lhs] = None

The python None value will translate to null when using json.dumps()
One could also just drop the else statement and simply get no name/value pair for this line.

@albop
Copy link
Owner

albop commented Feb 14, 2019

Makes sense. Can you submit a PR ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants