I have a JSON object with a huge array of nested objects. Let us assume it consists of records of license plates for vehicles. It would contain necessary fields like licenseID, issuingState, dateOfIssue, driverID etc.

What I am having problem with is how I should store data that is only used for exceptional cases, like a field for representing if the license plate is for foreign embassies (isEmbassyOwned) or if it is owned by a government entity (isGovernmentOwned) or if it is a learner license (isLearner) etc alongside fields with data types other than Boolean which would be empty or 0 and likewise when there is no information on that field. Let it be known that these exceptional scenarios would occur in less than 10% of total object instances.

I am facing confusion as to what format would be best for storing such type of data keeping balance between minimizing storage consumption and being human readable. Should I declare the fields for all objects regardless or only include them when they are not empty? Should I store them in a dedicated array instead, or maybe just introduce some code value to be used by a switch case operator in the interpreter? Or is there some other implementation I am not aware of?

  • dneaves@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    5 months ago

    When the enum reaches your JSON, it will have to be a string (as JSON does not have a dedicated “enum” type). But it at least ensures that languages parsing your JSON will should have a consistent set of strings to read.

    Consider this small bit of Elm code (which you may not be an Elm dev, and thats okay, but it’s the concept that you should look to get):

    -- A Directions "enum" type with four options:
    -- North, East, South, West
    type Directions
        = North
        | East
        | South
        | West
    
    -- How to turn each Directions into a String
    -- Which can then be encoded in JSON
    directionsToString : Directions -> String
    directionsToString direction =
        case direction of
            North -> "north"
            East  -> "east"
            South -> "south"
            West  -> "west"
    
    -- "Maybe Directions" since not all strings can be parsed as a Directions.
    -- The return will be "Just <something>" or "Nothing"
    directionsFromString : String -> Maybe Directions
    directionsFromString dirString =
        case dirString of
            "north" -> Just North
            "east"  -> Just East
            "south" -> Just South
            "west"  -> Just West
            _       -> Nothing
    

    The two functions (directionsFromString and directionsToString) are ready to be used as part of JSON handling, to read a String from a key and turn it into a Directions enum member, or to turn a Directions to a String and insert the string to a key’s value

    But all that aside, for your restructuring, and keeping with the license plate example, both type and license number could be contained in a small object. For example:

    {
        ...
        "licensePlate": {
            "type": "government"    <- an enum in the language parsing this
                                       but a string in JSON
            "plateNumber": "ABC123"
            ...
        }
        ...
    }