Question about managing json file changes

Recently for my project, I am looking for a way to manage json file updates.

I would like to know if there is a way to check if a json file contain a field, and if it’s not, not generate error message, but update the json file and add the missing field ?

You’ll have to implement custom logic I think.

A good way to model this might be to separate your serialization objects from the actual domain models, that is have all optional fields in the serialization models and then have a constructor for your domain models that take an instance of the serialization model and validate its fields for presence (and possibly other things), potentially normalize the values, fallback to default values and emit warnings as doing so. For writing back to the file the domain model could then emit back an instance of the serialization model populated from the internal consistent data.

Here is a script which will tell you if the key is in a json file.

require "json"

json = JSON.parse("<file>"))

def search(key_word, json)
  if json.as_h?
    if json[key_word]?
      return true
      json.as_h.each do |key, value|
        if value.as_h?
          search(key_word, value)
  elsif json.as_a?
    json.as_a.each do |value|
      if value.as_a?
        search(key_word, value)

p search("status", json)

I don’t know the performance of this, but this will search through an entire json file.
It will return true if it found it, otherwise nil.

I would try to leverage .

I thought there were not crystal implementation yet (since JSON Schema Validator · Issue #75 · crystal-community/crystal-libraries-needed · GitHub is still open) but there are some attempts in GitHub - kefahi/ Primordial attempt at validation with json-schema in Crystal .

Either way, if there is no crystal tooling, you might be able to solve the problem with some other tooling.

A json schema, besides defining the properties you want and getting a validation error, it allows you to define a default value. This default value is rarely (or never) used by validation. But other tools might used. In practice it seems to highlight for API what value would be semantically equivalent to not provide the given property.

I think that if the validation error is a missing object that happens to have a default value, then you can automate a fix.

Also, Quicktype can generate Crystal code from JSON Schemas, though I haven’t tested it extensively. Presumably once you have the Crystal code generated, you can go from there.

EDIT: Okay, I actually don’t think this helps with OP’s issue, but I’m leaving my reply in case someone searches the forum re JSON Schemas.