Home > OS >  How formalize JSON Document Updates
How formalize JSON Document Updates

Time:07-15

Assume, I have a nested JSON document:

{
 id: 12,
 name: 'bob',
 address: {
    street: 'main',
    code:1234
 }
}

I want to pubish changes of the document via a Message Broker such as Kafka. How should those JSON updates be formalized? Are there guidelines? Can you provide a good resource (I haven't found any)?

Assume, that the street name is changed, the following would come in my mind:

1: Keep the JSON structure and add the affected fields

{
 id: 12,
 address: {
    street: 'new street name'
 }
}

2: Custom JSON

{
 id: 12,
 updates: [{
    field: 'address.street',
    new_content: 'new street name'
 }]
}

CodePudding user response:

Easiest would be to find inspiration in REST API, as there are standards and best-practices already.

For changes you have: PUT, POST, DELETE, PATCH methods. The one you have described as "Custom JSON" is PATCH (set of instructions), but it seems kind of overengineering for simple update.

Therefore I would introduce "PUT" or "UPDATE" type which means you should simply update existing document with new fields, therefore:

{
 id: 12,
 type: "PUT",
 body: {
   address: {
      street: 'new street name'
   }
 }
}

As you dont have "headers", query or URL parameters in messages compare to HTTP request, you have to add such metadata inside the message itself and then know what part is the "metadata" and what is the "body".

There is also option to distinguish what to do by the "queue name" and that replaces the metadata part. So if you send message to i.e. "UpdatePerson" queue, you know that everything that goes there must update the Person, therefore this is sufficient:

{
 id: 12,
 address: {
    street: 'new street name'
 }
}
  •  Tags:  
  • json
  • Related