Skip to content

Markdown to JSON processing badly handles empty values #23

@SuperMayo

Description

@SuperMayo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions