molsonkiko / JsonToolsNppPlugin

A Notepad++ plugin providing tools for JSON like linting, querying, a tree view, and CSV conversion.
Apache License 2.0
70 stars 9 forks source link

Pretty Print restructures the actual JSON string #65

Closed brianreinhold closed 1 month ago

brianreinhold commented 1 month ago

When I do a 'pretty print' of a compressed JSON string the structure is changed. It looks as if the tags are alphabetized. That behavior causes problems because in a certain standard the order of the tags is specified, and though the compressed form is correct, the pretty print form is wrong and illegal.

For example here is what I should get:

"identifier": [
    {
        "type": {
            "coding": [
                {
                    "system": "http://terminology.hl7.org/CodeSystem/v2-0203",
                    "code": "MR",
                    "display": "Medical record number"
                }
            ]
        },
        "system": "urn:oid:1.2.3.4.5.6.7.8.10",
        "value": "sisansarahId"
    }
],

But here is what I actually get:

"identifier": [
    {
        "system": "urn:oid:1.2.3.4.5.6.7.8.10",
        "type": {
            "coding": [
                {
                    "code": "MR",
                    "display": "Medical record number",
                    "system": "http://terminology.hl7.org/CodeSystem/v2-0203"
                }
            ]
        },
        "value": "sisansarahId"
    }
],

The first form is legal for the standard, the second one is not. Is there a way to get the pretty print to format exactly what is given?

molsonkiko commented 1 month ago

Set the sort_keys setting to True. This will cause the "tags" (I prefer to call them "keys") to be sorted alphabetically as you desire.

But I think you have a bigger problem here; the order of the keys of a JSON object should not matter, because most programming languages represent it as an unordered map. If you have an internal process that breaks if the keys of a JSON object are not in a certain order, the process is at fault, not JsonTools.

brianreinhold commented 1 month ago

I want the order of the keys to be those that I have ordered. I do NOT want them alphabetized.

The HL7 FHIR standard originally wanted the order of the keys to be as defined in the standard. They originally had that requirement for both JSON and xml, but it was soon realized that many JSON libraries could not handle that and would not do that. However, since I am writing the JSON without a library I can satisfy the order requirement in JSON. The requirement remains for xml.

molsonkiko commented 1 month ago

I want the order of the keys to be those that I have ordered. I do NOT want them alphabetized.

@brianreinhold My mistake, I guess I was a bit confused by your examples. In that case, sort_keys should be set to False. Since this plugin is implmented in C#, this will cause key order to be preserved when reformatting.

By the way, you may want to upgrade to the latest version; given that sort_keys appears to be True by default it would seem that you are using an older version.

The HL7 FHIR standard originally wanted the order of the keys to be as defined in the standard.

Interesting, I didn't know that.

brianreinhold commented 1 month ago

Okay, I just installed it into NPP from the plugin manager. I will check for an update. Low and behold there was one!!