Why I stopped using JSON for my APIs

(aloisdeniel.com)

112 points | by barremian 12 hours ago

39 comments

  • davedx 14 minutes ago
    "Ultra-efficient"

    Searched the article, no mention of gzip, and how most of the time all that text data (html, js and css too!) you're sending over the wire will be automatically compressed to...... an efficient binary format!

    So really, the author should compare protobufs to gzipped JSON

    • chillfox 3 minutes ago
      Last time I was evaluating different binary serialization formats for an API I was really hoping to get to use one of the cool ones, but gzipped JSON just beat everything and it wasn't even close.
  • umvi 1 minute ago
    Protobufs are a pain to debug and maintain compared to json and modern browsers support zstd compression making json "efficient"
  • dfabulich 7 hours ago
    > With JSON, you often send ambiguous or non-guaranteed data. You may encounter a missing field, an incorrect type, a typo in a key, or simply an undocumented structure. With Protobuf, that’s impossible. Everything starts with a .proto file that defines the structure of messages precisely.

    This deeply misunderstands the philosophy of Protobuf. proto3 doesn't even support required fields. https://protobuf.dev/best-practices/dos-donts/

    > Never add a required field, instead add `// required` to document the API contract. Required fields are considered harmful by so many they were removed from proto3 completely.

    Protobuf clients need to be written defensively, just like JSON API clients.

    • cookiengineer 1 hour ago
      Isn't the core issue just language and implementation differences of clients vs servers here?

      I went all in with Go's Marshalling concept, and am using my Gooey framework on the client side nowadays. If you can come around Go's language limitations, it's pretty nice to use and _very_ typesafe. Just make sure to json:"-" the private fields so they can't be injected.

      [1] shameless drop https://github.com/cookiengineer/gooey

    • klysm 7 hours ago
      It’s also conflating the serialization format with contracts
      • ablob 2 hours ago
        I feel like that's fine since both things go hand in hand anyway. And if choosing the JSON-format comes with a rather high amount of contract-breaches it might just be easier to switch that instead of fixing the contract.
        • peterkelly 2 hours ago
          Unless a violation of that contract can lead to a crash or security vulnerability...
          • ablob 22 minutes ago
            The post is about changing the serialization-format so enforcing contracts becomes esier; and I am defending the post, so I don't understand what you're hinting at here.
      • shortrounddev2 5 hours ago
        Most web frameworks do both at the same time to the point where having to write code which enforced a type contract after deserializing is a delabreaker for me. I eant to be able to define my DTOs in one place, once, and have it both deserialize and enforce types/format. Anything else is code smell
        • Seattle3503 2 hours ago
          I'm in the same boat. I mostly write Rust and Python. Using serde_json and Pydantic, you get deserialization and validation at the same time. It allows you to de-serialize really "tight" types.

          Most of my APIs are internal APIs that accept breaking changes easily. My experience with protobufs is that it was created to solve problems in large systems with many teams and APIs, where backwards compatibility is important. There are certainly systems where you can't "just" push through a breaking API change, and in those cases protobufs make sense.

          • masklinn 1 hour ago
            > My experience with protobufs is that it was created to solve problems in large systems with many teams and APIs

            Also significant distribution such that it’s impossible to ensure every system is updated in lockstep (at least not without significant downtime), and high tail latencies e.g. a message could be stashed into a queue or database and processed hours or days later.

  • gethly 5 minutes ago
    Reading the first few paragraphs and immediately seeing PB made me instantly think of "Every master was once a beginner.".

    When you'll go through your own journey, and inevitably end back with json, do write another blog post :) ... we've all been there.

  • pzmarzly 11 hours ago
    > With Protobuf, that’s impossible.

    Unless your servers and clients push at different time, thus are compiled with different versions of your specs, then many safety bets are off.

    There are ways to be mostly safe (never reuse IDs, use unknown-field-friendly copying methods, etc.), but distributed systems are distributed systems, and protobuf isn't a silver bullet that can solve all problems on author's list.

    On the upside, it seems like protobuf3 fixed a lot of stuff I used to hate about protobuf2. Issues like:

    > if the field is not a message, it has two states:

    > - ...

    > - the field is set to the default (zero) value. It will not be serialized to the wire. In fact, you cannot determine whether the default (zero) value was set or parsed from the wire or not provided at all

    are now gone if you stick to using protobuf3 + `message` keyword. That's really cool.

    • connicpu 11 hours ago
      Regardless of whether you use JSON or Protobuf, the only way to be safe from version tears in your serialization format is to enforce backwards compatibility in your CI pipeline by testing the new version of your service creates responses that are usable by older versions of your clients, and vice versa.
    • brabel 11 hours ago
      No type system survives going through a network.
      • dhussoe 10 hours ago
        yes, but any sane JSON parsing library (Rust Serde, kotlinx-serialization, Swift, etc.) will raise an error when you have the wrong type or are missing a required field. and any JSON parsing callsite is very likely also an IO callsite so you need to handle errors there anyways, all IO can fail. then you log it or recover or whatever you do when IO fails in some other way in that situation.

        this seems like a problem only if you use JSON.parse or json.loads etc. and then just cross your fingers and hope that the types are correct, basically doing the silent equivalent of casting an "any" type to some structure that you assume is correct, rather than strictly parsing (parse, don't validate) into a typed structure before handing that off to other code.

        • koakuma-chan 7 hours ago
          > strictly parsing (parse, don't validate)

          That's called validating? Zod is a validation library.

          But yeah, people really need to start strictly parsing/validating their data. One time I had an interview and I was told yOu DoN'T tRuSt YoUr BaCkeNd?!?!?!?

          • dhussoe 7 hours ago
            "parse don't validate" is from: https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...

            looking at zod (assuming https://zod.dev) it is a parsing library by that definition — which isn't, like, an official definition or anything, one person on the internet came up with it, but I think it is good at getting the principle across

            under these definitions a "parser" takes some input and returns either some valid output (generally a more specific type, like String -> URL) or an error, whereas a "validator" just takes that input and returns a boolean or throws an error or whatever makes sense in the language.

            eta: probably part of the distinction here is that since zod is a JS library the actual implementation can be a "validator" and then the original parsed JSON input can just be returned with a different type. "parse don't validate" is (IMO) more popular in languages like Rust where you would already need to parse the JSON to a language-native structure from the original bytes, or to some "JSON" type like https://docs.rs/serde_json/latest/serde_json/enum.Value.html that are generally awkward for application code (nudging you onto the happy parsing path).

            • koakuma-chan 6 hours ago
              I like your message and I think that you are right on everything.
              • dhussoe 6 hours ago
                yeah I have repeatedly had things like "yOu DoN'T tRuSt YoUr BaCkeNd?!?!?!?" come up and am extremely tired of it when it's 2025 and we have libraries that solve this problem automatically and in a way that is usually more ergonomic anyways... I don't do JS/TS so I guess just casting the result of JSON.parse is sort of more convenient there, but come on...
                • koakuma-chan 6 hours ago
                  Yes, I know right? You are so lucky to not do JS/TS—those people are incredible. Finally, someone who understands me.
        • socalgal2 2 hours ago
          It's common to validate in JS land as well

          https://github.com/ajv-validator/ajv

  • pyrolistical 11 hours ago
    Compressed JSON is good enough and requires less human communication initially.

    Sure it will blow up in your face when a field goes missing or value changes type.

    People who advocate paying the higher cost ahead of time to perfectly type the entire data structure AND propose a process to do perform version updates to sync client/server are going to lose most of the time.

    The zero cost of starting with JSON is too compelling even if it has a higher total cost due to production bugs later on.

    When judging which alternative will succeed, lower perceived human cost beats lower machine cost every time.

    This is why JSON is never going away, until it gets replaced with something with even lower human communication cost.

    • barremian 9 hours ago
      > When judging which alternative will succeed, lower perceived human cost beats lower machine cost every time.

      Yup this is it. No architect considers using protos unless there is an explicit need for it. And the explicit need is most times using gRPC.

      Unless the alternative allows for zero cost startup and debugging by just doing `console.log()`, they won't replace JSON any time soon.

      Edit: Just for context, I'm not the author. I found the article interesting and wanted to share.

      • duped 4 hours ago
        Print debugging is fine and all but I find that it pays massive dividends to learn how to use a debugger and actually inspect the values in scope rather than guessing which are worth printing. It also is useless when you need to debug a currently running system and can't change the code.

        And since you need to translate it anyway, there's not much benefit in my mind to using something like msgpack which is more compact and self describing, you just need a decoder to convert to json when you display it.

        • themafia 24 minutes ago
          > rather than guessing

          I'm not guessing. I'm using my knowledge of the program and the error together to decide what to print. I never find the process laborious and I almost always get the right set of variables in the first debug run.

          The only time I use a debugger is when working on someone else's code.

    • morshu9001 10 hours ago
      I've gone the all-JSON route many times, and pretty soon it starts getting annoying enough that I lament not using protos. I'm actually against static types in languages, but the API is one place they really matter (the other is the DB). Google made some unforced mistakes on proto usability/popularity though.
      • almosthere 3 hours ago
        why are you against static types in languages?

        I once converted a fairly large JS codebase to TS and I found about 200 mismatching names/properties all over the place. Tons of properties we had nulls suddenly started getting values.

        • MobiusHorizons 2 hours ago
          Sounds like this introduced behavior changes. How did you evaluate if the new behavior was desirable or not? I’ve definitely run into cases where the missing fields were load bearing in ways the types would not suggest, so I never take it for granted that type error in prod code = bug
    • esafak 11 hours ago
      It won't go away in the same way COBOL won't. That does not mean we should be using it everywhere for greenfield projects.
    • DyslexicAtheist 10 hours ago
      > People who advocate paying the higher cost ahead of time to perfectly type the entire data structure AND propose a process to do perform version updates to sync client/server are going to lose most of the time.

      that's true. But people also rather argue about security vulnerabilities than getting it right from the get-go. Why spend an extra 15 mins effort during design when you can spend 3 months revisiting the ensuing problem later.

      • jstanley 10 hours ago
        Alternatively: why spend an extra 15 mins on protobuf every other day, when you can put off the 3-month JSON-revisiting project forever?
        • OccamsMirror 2 hours ago
          I use ConnectRPC (proto). I definitely do not spend any extra time. In fact the generated types for my backend and frontend saves me time.
  • codewritero 11 hours ago
    I love to see people advocating for better protocols and standards but seeing the title I expected the author to present something which would be better in the sense of supporting the same or more use cases with better efficiency and/or ergonomics and I don't think that protobuf does that.

    Protobuf has advantages, but is missing support for a tons of use cases where JSON thrives due to the strict schema requirement.

    A much stronger argument could be made for CBOR as a replacement for JSON for most use cases. CBOR has the same schema flexibility as JSON but has a more concise encoding.

    • port11 11 hours ago
      I think the strict schema of Protobuf might be one of the major improvements, as most APIs don't publish a JSON schema? I've always had to use ajv or superstruct to make sure payloads match a schema, Protobuf doesn't need that (supposedly).
    • youngtaff 11 hours ago
      We need browsers to support CBOR APIs… and it shouldn’t be that hard as they all have internal implementations now
      • deathanatos 6 hours ago
        I suppose I should publish this, but a WASM module, in Rust, which just binds [ciborium] into JS only took me ~100 LoC. (And by this I mean that it effectively provides a "cbor_load" function to JS, which returns JS objects; I mention this just b/c I think some people have the impression that WASM can't interact with JS except by serializing stuff to/from bytestrings and/or JSON, which isn't really the whole story now with refs.)

        But yes, a native implementation would save me the trouble!

        [ciborium]: a Rust CBOR library; https://docs.rs/ciborium/latest/ciborium/

  • rock_artist 1 hour ago
    I feel few points weren’t addressed in the article.

    1. Size, biggest problem with JSON can happen when things gets too big. So here other formats might be better. Yet, as a reminder JSON has the binary version named BSON.

    2. Zero batteries. JSON is readable by humans but also almost self explanatory format. Most languages has built in or quick drop in for json. Still, it’s easy to implement a limited JSON parser from scratch when in need (eg. Pure on func in C on a tiny device).

    Working with Protobuf and MsgPack in the past, You have much more tooling involved especially if data passes between parts written in different languages.

    3. Validation, JSON is simple. But there are solutions such as JSON Schema.

    • paulddraper 56 minutes ago
      BSON is not simply a binary encoding of JSON.

      It is a JSON superset with binary encoding, created by MongoDB. (And there is even a JSON encoding of BSON, called extended JSON.)

      AFAIK there is no widely adopted binary pure adaptation of JSON. (There are application-specific storage formats, like PostgreSQL JSONB, or SQLite JSONB.)

      ——-

      Moreover, JSON is relatively compact. BSON or other self descriptive binary formats are often around the same size of JSON. MessagePack aggressively tries to be compact and is, depending on the data. BSON doesn’t try to be compact, rather it improves the parse speed.

  • brabel 11 hours ago
    Mandatory comment about ASN.1, a protocol from 1984, already did what Protobuf does, with more flexibility. Yes, it's a bit ugly but if you stick to the DER encoding it's really not worse than Protbuf at all. Check out the Wikipedia example:

    https://en.wikipedia.org/wiki/ASN.1#Example_encoded_in_DER

    Protobuf is ok but if you actually look at how the serializers work, it's just too complex for what it achieves.

    • theamk 5 hours ago
      ASN.1 has too much stuff. The moment you write "I made ASN.1 decoder/encoder", someone will throw TeletexString or BMPString at it. Or inheritance, as morshu9001 sad. So at this point:

      - You can support all those features, and your ASM.1 library will be horribly bloated and over-engineered.

      - You can support your favorite subset, but then you cannot say it's ASN.1 anymore. It will be "ASN.brabel", which only has one implementation (yours). And who wants that?

      (unless you are Google and have immense developer influence... But in this case, why not design things from scratch, since we are making all-new protocol anyway?)

      • themafia 13 minutes ago
        > someone will throw TeletexString or BMPString

        ASCII with escapes and UCS-2.

        > horribly bloated and over-engineered.

        It's no more or less complicated than XML, JSON or CSV. Which is why you can use ASN.1 to serialize to and from all these formats. ASN.1 provides you an additional level of schema above these. It simply allows you to describe your problem.

        I find ASN.1 far more sane and useful than something like JSON Schema which is just as "bloated and over-engineered." It turns out describing data is not a simple problem.

    • zzo38computer 11 hours ago
      I also think ASN.1 DER is better (there are other formats, but in my opinion, DER is the only good one, because BER is too messy). I use it in some of my stuff, and when I can, my new designs also use ASN.1 DER rather than using JSON and Protobuf etc. (Some types are missing from standard ASN.1 but I made up a variant called "ASN.1X" which adds some additional types such as key/value list and some others. With the key/value list type added, it is now a superset of the data model of JSON, so you can convert JSON to ASN.1X DER.)

      (I wrote a implementation of DER encoding/decoding in C, which is public domain and FOSS.)

    • morshu9001 11 hours ago
      ASN.1 is way overengineered to the point of making it hard to support. You don't need inheritance for example.
      • zzo38computer 10 hours ago
        it is not necessary to use or to implement all of the data types and other features of ASN.1; you can implement only the features that you are using. Since DER uses the same framing for all data types, it is possible to skip past any fields that you do not care about (although in some cases you will still need to check its type, to determine whether or not an optional field is present; fortunately the type can be checked easily, even if it is not a type you implement).
        • morshu9001 10 hours ago
          Yes but I don't want to worry about what parts of the spec are implemented on each end. If you removed all the unnecessary stuff and formed a new standard, it'd basically be protobuf.
          • zzo38computer 10 hours ago
            I do not agree. Which parts are necessary depends on the application; there is not one good way to do for everyone (and Protobuf is too limited). You will need to implement the parts specific to your schema/application on each end, and if the format does not have the data types that you want then you must add them in a more messy way (especially when using JSON).
            • morshu9001 9 hours ago
              In what ASN1 application is protobuf spec too limited? I've used protobuf for tons of different things, it's always felt right. Though I understand certain encodings of ASN1 can have better performance for specific things.
              • cryptonector 1 hour ago
                Open types, constrained types, parameterized types, not needing tags, etc.
              • zzo38computer 9 hours ago
                Numbers bigger than 64-bits, character sets other than Unicode (and ASCII), OIDs, etc.
                • morshu9001 7 hours ago
                  These are only scalars that you'd encode into bytes. I guess it's slightly annoying that both ends have to agree on how to serialize rather than protobuf itself doing it, but it's not a big enough problem.

                  Also I don't see special ASN1 support for non-Unicode string encodings, only subsets of Unicode like ascii or printable ascii. It's a big can of worms once you bring in things like Latin-1.

                  • zzo38computer 6 hours ago
                    ASN.1 has support for ISO 2022 as well as ASCII and Unicode (ASCII is a subset of Unicode as well as a subset of ISO 2022). (My own nonstandard extensions add a few more (such as TRON character code and packed BCD), and the standard unrestricted character string type can be used if you really need arbitrary character sets.) (Unicode is not a very good character set, anyways.)

                    Also, DER allows to indicate the type of data within the file (unless you are using implicit types). Protobuf has only a limited case of this (you cannot always identify the types), and it requires different framing for different types. However, DER uses the same framing for all types, and strings are not inherently limited to 2GB by the file format.

                    Furthermore, there are other non-scalar types as well.

                    In any of these cases, you do not have to use all of the types (nor do you need to implement all of the types); you only need to use the types that are applicable for your use.

                    I will continue to use ASN.1; Protobuf is not good enough in my opinion.

                    • cryptonector 1 hour ago
                      To be fair, if you don't need to support anything other than Unicode, then this is not an advantage, and over time we're all going to need non-Unicode less and less. That said I'm a big fan of ASN.1 (see my comment history).
    • cryptonector 1 hour ago
      Thank you! Now I don't have to be the one saying this. Props if you use OER over DER. But since OP needs available tooling they might as well go to flatbuffers, which is much better than PB much like OER is much better than DER.
    • strongpigeon 10 hours ago
      > Protobuf is ok but if you actually look at how the serializers work, it's just too complex for what it achieves.

      Yeah. I do remember a lot of workloads at Google where most of the CPU time was spent serializing/deserializing protos.

      • ses1984 5 hours ago
        I feel like most high throughput distributed systems eventually reach a point where some part of it is constrained by de/serialization.

        Not much is faster than protobuf except for zero copy formats.

    • adastra22 2 hours ago
      ASN.1 is a nightmare, and I would never use it for a greenfield project.
      • cryptonector 1 hour ago
        I... wouldn't use it for a greenfield project either unless I got good at porting Luke Howard's Swift ASN.1 stack to whatever language I might be using that isn't C. For C I'd just use Heimdal's awesome ASN.1 compiler and be done. Even then I would be tempted to use flatbuffers instead, or else I'd have to go implement OER (a bunch of work I don't really care to do).

        The problem with ASN.1 -- the only real problem with ASN.1, is lack of excellent tooling.

    • bloppe 11 hours ago
      What makes it too complex in your opinion?
    • dgan 11 hours ago
      I honestly looked up for a encoder/decoder for python/c++ application, and couldnt find anything usable; i guess i would need to contact the purchase department for a license (?), while with protobuf i can make the decision myself & all alone
    • pphysch 11 hours ago
      > ASN.1, a protocol from 1984, already did what Protobuf does, with more flexibility.

      After working heavily with SNMP across a wide variety of OEMs, this flexibility becomes a downside. Or SNMP/MIBs were specified at the wrong abstraction level, where the ASN.1 flexibility gives mfgs too much power to do insane and unconventional things.

      • cryptonector 1 hour ago
        That's not ASN.1's fault though.
      • morshu9001 10 hours ago
        Yeah same, ASN.1 was a nightmare when I was dealing with LTE
        • cryptonector 1 hour ago
          I've been working on and with Kerberos and PKIX for decades. I don't find ASN.1 to be a problem as long as you have good tooling or are willing to build it. The specs are a pleasure to read -- clear, concise, precise, and approachable (once you have a mental model for it anyways).

          Of course, I am an ASN.1 compiler maintainer, but hey, I had to become one because the compiler I was using was awesome but not good enough, so I made it good enough.

          I'm curious what made it a nightmare for you.

  • JoshMock 7 hours ago
    Protobuf is a great format with a lot of benefits, but it's missing one that I wish it could support: zero-copy. The ability to transport data between processes, services and languages with effectively zero time spent on serialization and deserialization.

    It appears possible in some cases but it's not universally the case. Which means that similar binary transport formats that do support zero-copy, like Cap'n Proto, offer most or all of the perks described in this post, with the addition of ensuring that serialization and deserialization are not a bottleneck when passing data between processes.

    • pornel 2 hours ago
      It depends how you actually use the messages. Zero-copy can be slowing things down. Copying within L1 cache is ~free, but operating on needlessly dynamic or suboptimal data structures can add overheads everywhere they're used.

      To actually literally avoid any copying, you'd have to directly use the messages in their on-the-wire format as your in-memory data representation. If you have to read them many times, the extra cost of dynamic getters can add up (the format may cost you extra pointer chasing, unnecessary dynamic offsets, redundant validation checks and conditional fallbacks for defaults, even if the wire format is relatively static and uncompressed). It can also be limiting, especially if you need to mutate variable-length data (it's easy to serialize when only appending).

      In practice, you'll probably copy data once from your preferred in-memory data structures to the messages when constructing them. When you need to read messages multiple times at the receiving end, or merge with some other data, you'll probably copy them into dedicated native data structs too.

      If you change the problem from zero-copy to one-copy, it opens up many other possibilities for optimization of (de)serialization, and doesn't keep your program tightly coupled to the serialization framework.

    • jonny_eh 6 hours ago
      Is that a format/serialization issue, or library/implementation issue?
      • ElectricalUnion 4 hours ago
        Serialization issue. From the Introduction to Cap’n Proto:

        "Cap’n Proto is INFINITY TIMES faster than Protocol Buffers. (...) there is no encoding/decoding step. The Cap’n Proto encoding is appropriate both as a data interchange format and an in-memory representation, so once your structure is built, you can simply write the bytes straight out".

        I take it as a rationalization of what OLE Compound File Binary - internal Microsoft Office memory structures serialized "raw" as file format - would look like if they paid more attention to being backward and forward compatible and extensible.

        • TillE 2 hours ago
          Google has a library/format for that too, with FlatBuffers. Different use cases and advantages really, not clearly better/worse.
      • __s 4 hours ago
  • written-beyond 11 hours ago
    Idk I built a production system and ensured all data transfers, client to server and server to client were proto buf and it was a pain.

    Technically, it sounds really good but the actual act of managing it is hell. That or I need a lot of practice to use them, at that point shouldn't I just use JSON and get on with my life.

    • barremian 9 hours ago
      I think the it-just-works nature and human readability for debugging JSON cannot be overstated. Most people and projects are content to just use JSON even if protos offer some advantages, if not only to save time and resources.

      Whether the team saves times in the longer when using protos is a question in its own.

    • Arainach 11 hours ago
      What issues did you have? In my experience, most things that could be called painful with protobuf would be bigger pains with things like JSON.

      Making changes to messages in a backwards-compatible way can be annoying, but JSON allowing you to shoot yourself in the foot will take more time and effort to fix when it's corrupting data in prod than protobuf giving you a compile error would.

      • written-beyond 10 hours ago
        Well at the bare minimum setting up proto files and knowing where they live across many projects.

        If they live in their own project, making a single project be buildable with a git clone gets progressively more complex.

        You now need sub modules to pull in your protobuf definitions.

        You now also need the protobuf tool chain to be available in your environment you just cloned to. If that environment has the wrong version the build fails, it starts to get frustrating pretty fast.

        Compare that to json, yes I don't get versioning and a bunch of other fancy features but... I get to finish my work, build and test pretty quickly.

  • socalgal2 2 hours ago
    https://github.com/ajv-validator/ajv

    138 million downloads from npm in the last week. Yes, you can validate your JSON

  • morshu9001 10 hours ago
    Protos are great. Last time I did a small project in NodeJS, I set up a server that defines the entire API in a .proto and serves each endpoint as either proto or json, depending on the content header. Even if the clients want to use json, at least I can define the whole API in proto spec instead of something like Swagger.

    So my question is, why didn't Google just provide that as a library? The setup wasn't hard but wasn't trivial either, and had several "wrong" ways to set up the proto side. They also bait most people with gRPC, which is its own separate annoying thing that requires HTTP/2, which even Google's own cloud products don't support well (e.g App Engine).

    P.S. Text proto is also the best static config language. More readable than JSON, less error-prone than YAML, more structure than both.

    • c-cube 9 hours ago
      That's what Twirp (https://github.com/twitchtv/twirp) is about. Protobuf or JSON, over any HTTP, with a simple URL schema. It's fairly simple.
      • rileymichael 4 hours ago
        highly recommend twirp, even in current year. connectrpc seems to have stalled so there isn't a ton of languages w/server support, and because of the grpc interop on top of their own protocol it's a bit of an undertaking to roll your own.

        the twirp spec however is so simple you can throw together your own code generator in an afternoon for whatever language you want.

    • noctune 10 hours ago
      You might be interested in https://connectrpc.com/. It's basically what you describe, though it's not clear to me how well supported it is.
      • morshu9001 10 hours ago
        Yeah that one looked good. I don't remember why I didn't use it that time, maybe just felt it was easy enough to DIY that I didn't feel like using another dep (given that I already knew express and proto in isolation). The thing is, Google themselves had to lead the way on this if they wanted protobuf to be mainstream like JSON.
  • recursivecaveat 9 hours ago
    My dream binary format is schema driven, as compact and efficient as Capt Proto or such, but just optionally embeds the entire schema into the message. Then we can write a vim plugin that just opens the file in human readable form without having to fish for the schema. Whenever I am using binary formats, it's because I have a list of millions of objects of the same types. Seems to me that you may as well tack 1KB of schema onto a 2GB message and make it self-describing so everyone's life is easier.
  • PunchyHamster 6 hours ago
    "I've used this optimization technique to make app faster"

    The app 20req/sec

    The app after optimizations: 20req/sec (It waits for db query anyway)

    • PantaloonFlames 4 hours ago
      Yes. Proto makes sense when the request rate is much higher and the network is constrained.

      Otherwise, json is sufficient.

  • lenkite 1 hour ago
    There is no mention in this thread of how the author is using Dart and Shelf for his REST API's. Code is rather readable and elegant. This is not a combo I have every tried before. Does anyone have any experience of how it compares versus REST services written in Go/Rust/Python ?
  • beders 2 hours ago
    Know the consumer of your API.

    If that is just your team, use whatever tech gets you there quick.

    However, if you need to provide some guarantees to a second or third party with your API, embrace standards like JSON, even better, use content negotiation.

  • sergiotapia 11 minutes ago
    Personally, I looked into protobuf for our Elixir/React Native wombocombo but the second I realized we would have to deploy app updates when we added or removed a field from the response structure it became a non-starter.

    I can't imagine using protobuf when you're in the first 5 years of a product.

  • wg0 10 hours ago
    Protos don't work out of the box in any browsers as far as I checked last time unless you're willing to deploy a proxy in front to do the translation and it requires extra dependency on the browser as well.

    Plus - tooling.

    JSON might not be simpler or strict but it gets the job done.

    If JSON's size or performance is causing you to go out of business, you surely have bigger problems than JSON.

    • taftster 1 hour ago
      Right, I find the use of protobuf lacking with direct support in the browser. Since JSON is a native data encoding format of the browser (effectively), it's just easier to have a JSON-based API.

      Yes, there are abstractions or other hacks that would bolt on protobuf support in the browser, but that's not ideal in my mind.

      Protobuf is an ideal exchange format when you're not dealing with the public as a whole. That is, a private or corporate API for your data processing pipelines, etc.

  • lazy_afternoons 1 hour ago
    IIRC from the red wild boar book, JSONs biggest win is that it got the adoption.

    Getting everyone to agree on a standard was/is/will be the tougher part.

  • cryptonector 1 hour ago
    If you're gonna switch from JSON to... PB, then you might as well switch to flatbuffers before you realize it's better than PB and save yourself a lot of trouble.
  • brunoluiz 7 hours ago
    I am curious why the author did not consider ConnectRPC (http://connectrpc.com/), which could be a great middle ground since it is compatible with both Protobuf and JSON served APIs. It is developed by Buf, which has been a leader Protobuf tooling.
  • coffeeaddict1 8 hours ago
    > protobuf

    As an aside, like all things Google, their C++ library is massive (14mb dll) and painful to build (takes nearly 10 minutes on my laptop).

    • mb7733 28 minutes ago
      Yeah and (1) the codegen produces massive headers that slow compilation of anything that touches them (2) the generated classes are really awkward to use. Not a big fan of the experience of protobuffer generated code in a large C++ code base.

      It's lead to a huge layer of adapters and native c++ classes equivalent to the protobuffers classes to try and mitigate these issues.

  • teleforce 7 hours ago
    Kudos to the poster and the author of this article. I think this is by far the most insightful technical post I've read this year on HN.

    >If you develop or use an API, there’s a 99% chance it exchanges data encoded in JSON.

    Just wondering if the inherent defiencies of JSON can somewhat be improved by CUE lang since the former is very much pervasive and the latter understand JSON [1],[2].

    [1] Configure Unify Execute (CUE): Validate, define, and use dynamic and text‑based data:

    https://cuelang.org/

    [2] Cue – A language for defining, generating, and validating data:

    https://news.ycombinator.com/item?id=20847943

    • Joel_Mckay 6 hours ago
      BSON dealt with a lot of the JSON limitations, and ambiguous type issues.

      Batching with message pooling to a transaction payload size limit actually made it performant. =3

  • Jemaclus 11 hours ago
    "Better than JSON" is a pretty bold claim, and even though the article makes some great cases, the author is making some trade-offs that I wouldn't make, based on my 20+ year career and experience. The author makes a statement at the beginning: "I find it surprising that JSON is so omnipresent when there are far more efficient alternatives."

    We might disagree on what "efficient" means. OP is focusing on computer efficiency, where as you'll see, I tend to optimize for human efficiency (and, let's be clear, JSON is efficient _enough_ for 99% of computer cases).

    I think the "human readable" part is often an overlooked pro by hardcore protobuf fans. One of my fundamental philosophies of engineering historically has been "clarity over cleverness." Perhaps the corollary to this is "...and simplicity over complexity." And I think protobuf, generally speaking, falls in the cleverness part, and certainly into the complexity part (with regards to dependencies).

    JSON, on the other hand, is ubiquitous, human readable (clear), and simple (little-to-no dependencies).

    I've found in my career that there's tremendous value in not needing to execute code to see what a payload contains. I've seen a lot of engineers (including myself, once upon a time!) take shortcuts like using bitwise values and protobufs and things like that to make things faster or to be clever or whatever. And then I've seen those same engineers, or perhaps their successors, find great difficulty in navigating years-old protobufs, when a JSON payload is immediately clear and understandable to any human, technical or not, upon a glance.

    I write MUDs for fun, and one of the things that older MUD codebases do is that they use bit flags to compress a lot of information into a tiny integer. To know what conditions a player has (hunger, thirst, cursed, etc), you do some bit manipulation and you wind up with something like 31 that represents the player being thirsty (1), hungry (2), cursed (4), with haste (8), and with shield (16). Which is great, if you're optimizing for integer compression, but it's really bad when you want a human to look at it. You have to do a bunch of math to sort of de-compress that integer into something meaningful for humans.

    Similarly with protobuf, I find that it usually optimizes for the wrong thing. To be clear, one of my other fundamental philosophies about engineering is that performance is king and that you should try to make things fast, but there are certainly diminishing returns, especially in codebases where humans interact frequently with the data. Protobufs make things fast at a cost, and that cost is typically clarity and human readability. Versioning also creates more friction. I've seen teams spend an inordinate amount of effort trying to ensure that both the producer and consumer are using the same versions.

    This is not to say that protobufs are useless. It's great for enforcing API contracts at the code level, and it provides those speed improvements OP mentions. There are certain high-throughput use-cases where this complexity and relative opaqueness is not only an acceptable trade off, but the right one to make. But I've found that it's not particularly common, and people reaching for protobufs are often optimizing for the wrong things. Again, clarity over cleverness and simplicity over complexity.

    I know one of the arguments is "it's better for situations where you control both sides," but if you're in any kind of team with more than a couple of engineers, this stops being true. Even if your internal API is controlled by "us," that "us" can sometimes span 100+ engineers, and you might as well consider it a public API.

    I'm not a protobuf hater, I just think that the vast majority of engineers would go through their careers without ever touching protobufs, never miss it, never need it, and never find themselves where eking out that extra performance is truly worth the hassle.

    • PantaloonFlames 4 hours ago
      Yes to all of this.

      Also the “us” is ever-changing in a large enough system. There are always people joining and leaving the team. Always, many people are approximately new, and JSON lets them discover more easily.

    • Arainach 11 hours ago
      If you want human readable, there are text representations of protobuf for use at rest (checked in config files, etc.) while still being more efficient over the wire.

      In terms of human effort, a strongly typed schema rather than one where you have to sanity check everything saves far more time in the long run.

    • Aldipower 11 hours ago
      Great writing, thanks. There are of course 2 sides as always. I think especially for larger teams and large projects Protobuf in conjunction with gRPC can play wisely with the backwards compatibility feature, which makes it very hard to break things.
  • _el1s7 6 hours ago
    Why I stopped caring about "Why I stopped [insert something widely used here]" click bait articles
  • bzmrgonz 3 hours ago
    What about TOON op? I understand that's the standard poised to takeover from Json. Your thoughts??
  • jtrn 10 hours ago
    I like Python-like indentation, but I usually read Python in an IDE or code blocks. JSON in a non-monospace environment might be problematic with some fonts. Hell, I pass JSON around in emails and word processors all the time.
  • keithnz 7 hours ago
    CBOR is a pretty good middle ground
  • spagoop 11 hours ago
    Is it just me or is this article insanely confusing? With all due respect to the author, please be mindful of copy editing LLM-assisted writing.

    There is a really interesting discussion underneath of this as to the limitations of JSON along with potential alternatives, but I can't help but distrust this writing due to how much it sounds like an LLM.

    • port11 11 hours ago
      I don't think it's LLM-generated or even assisted. It's kinda like how I write when I don't want to really argue a point but rather get to the good bits.

      Seems like the author just wanted to talk about Protobuf without bothering too much about the issues with JSON (though some are mentioned).

    • phyzome 7 hours ago
      It wasn't confusing, but yeah, it smelled strongly of LLMs.
    • pavel_lishin 11 hours ago
      > Is it just me or is this article insanely confusing?

      I didn't find it confusing.

      I found it unconvincing, but the argument itself was pretty clear. I just disagreed with it.

    • dkdcio 11 hours ago
      do you have any evidence that the author used a LLM? focusing on the content, instead of the tooling used to write the content, leads to a lot more productive discussions

      I promise you cannot tell LLM-generated content from non-LLM generated content. what you think you’re detecting is poor quality, which is orthogonal to the tooling used

      • spagoop 11 hours ago
        Fair point, to be constructive here, LLMs seem to love lists and emphasizing random words / phrases with bold. Those two are everywhere. Not a smoking gun but enough to tune out.

        I am not dismissing this as being slop and actually have no beef with using LLMs to write but yes, as you call out, I think it's just poorly written or perhaps I'm not the specific audience for this.

        Sorry if this is bad energy, I appreciate the write up regardless.

  • catchmeifyoucan 11 hours ago
    I wonder if we can write an API w/ JSON the usual way and change the final packaging to send it over protobuf.
    • lanigone 2 hours ago
      envoy (the proxy) can transcode RESTful APIs to internal grpc services and vice versa. you can even map like url params etc. to proto fields. it works well, even server streaming.
    • bglusman 11 hours ago
      Sure... https://protobuf.dev/programming-guides/json/

      I was pushing at one point for us to have some code in our protobuf parsers that would essentially allow reading messages in either JSON or binary format, though to be fair there's some overhead that way by doing some kind of try/catch, but, for some use cases I think it's worth it...

    • pstuart 11 hours ago
      If you're using Go then this framework let's you work with protobufs and gives you a JSON rest-like service for free: https://github.com/grpc-ecosystem/grpc-gateway
  • wilg 11 hours ago
    One of the best parts of Protobuf is that there's a fully compatible JSON serialization and deserialization spec, so you can offer a parallel JSON API with minimal extra work.
  • taco_emoji 11 hours ago
    i really hate this blog post format of posting the new thing you discovered as if it's objectively better than the previous thing. no it's not, you just like it better, which is FINE, just own it
  • _heimdall 6 hours ago
    I need to dig deeper into Protobuf. I've never quite understood the benefit of Protobug over XML.
  • loph 7 hours ago
    How many times has this problem been "solved"?

    https://en.wikipedia.org/wiki/DCE/RPC

    DCE/RPC worked in 1993, and still does today.

    Protocol buffers is just another IDL.

  • esafak 11 hours ago
    It's premature and maybe presumptuous of him to be advertising protobufs when he hasn't heard of the alternatives yet. I'll engage the article after he discovers them...
  • rizky05 2 hours ago
    [dead]
  • volemo 11 hours ago
    S-expressions exist since 1960, what more do you need? /s
  • dhussoe 10 hours ago
    I like that JSON parsing libraries (Serde etc.) allow you to validate nullability constraints at parse-time. Protobuf's deliberate lack of support for required fields means that either you kick that down to every callsite, or you need to build another parsing layer on top of the generated protobuf code.

    Now, there is a serde_protobuf (I haven't used it) that I assume allows you to enforce nullability constraints but one of the article's points is that you can use the generated code directly and:

    > No manual validation. No JSON parsing. No risk of type errors.

    But this is not true—nullability errors are type errors. Manual validation is still required (except you should parse, not validate) to make sure that all of the fields your app expects are there in the response. And "manual" validation (again, parse don't validate) is not necessary with any good JSON parsing library, the library handles it.