I'm not sure if this is a flaw in JWT specification or if I don't understand something. In JWT, the order of JSON key, value pairs matters when encoding and decoding tokens. Having {"alg": "HS256", "typ": "JWT"} headers produces different token compared to {"typ": "JWT", "alg": "HS256"}.
This seems weird to me because JSON specification states that the key, value pairs do not have an order: An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma). _source_.

Do you have an explanation for this?
Ok, I just figured out why this doesn't matter. As long as the secret is same, the token will work even though it looks different. The receiving part will use the secret to decode the token and even though the headers or payload are in different order, the end result is the same.
Most helpful comment
Ok, I just figured out why this doesn't matter. As long as the secret is same, the token will work even though it looks different. The receiving part will use the secret to decode the token and even though the headers or payload are in different order, the end result is the same.