Are keys in JSON case sensitive?

Of course, yes, you say. But this article would not exist if there was no question.

Also, this article will be useful to you if you use Tinkoff acquiring.

A little background. Some time ago, on one of my projects, I changed online acquiring to Tinkoff, and after debugging, strange errors began to appear with payment: the hash signature of the request was considered incorrect. Moreover, only on real payments, which further darkened the situation.

The problem is in the field Data json object that comes from the bank in the payment notification. In some cases it comes as Dataand in others DATA (in uppercase).

I reported the problem to Tinkoff support, and then something interesting began. Support claims that the case of keys in JSON does not matter:

Valery, hello, JSON is not case sensitive. From his point of view, DATA and Data are one and the same. The root of the problem here is your software, which is case sensitive.

Unfortunately, Tinkoff’s technical specialists failed to prove the opposite. Finding a mention of JSON case sensitivity in the specification is even more difficult, so I did what I always do: write code and check how it works.

Since JSON does not mean anything by itself, but is always processed by some programming language, it is easy to find out how certain languages ​​treat keys in different case in JSON.

JavaScript – as the most popular

When talking about JSON, the most popular language that uses it is JS.

const json = JSON.parse('{"Data":1,"DATA":2}');
console.log(json, json.Data); // {Data: 1, DATA: 2} 1

As you can see, the register of keys plays a role. Two different keys with different values. And if the documentation says the key Datathen DATA doesn’t fit, alas.

PHP – when there is no JS

Perhaps there will be a difference between an array and an object?

<?php
$json = json_decode('{"Data":1,"DATA":2}', true);
var_dump($json);
// array (size=2)
//  'Data' => int 1
//  'DATA' => int 2

$json = json_decode('{"Data":1,"DATA":2}', false);
var_dump($json);
// object(stdClass)[253]
//  public 'Data' => int 1
//  public 'DATA' => int 2

The situation is similar. Keys in different case are different keys. Converting to an array or an object doesn’t matter.

Go is a strongly typed language

Yet JS and PHP take a lot of liberties. Perhaps a strongly typed compiled language would behave differently?

var data map[string]interface{}
json.Unmarshal([]byte("{\"Data\":1,\"DATA\":2}"), &data)
fmt.Println(data) // map[DATA:2 Data:1]

And again, keys in a different register are different keys. We got a map with two different keys and values.

Conclusion

If you’re still unsure if JSON is case sensitive, just try similar code in your programming language.

I haven’t come across a language where JSON is case insensitive. If you know one, please let me know, it will be interesting.

PS I’m sure that Tinkoff developers also read Habr. Please pay attention to the problem. You have randomly changed the case of the Data key in the payment notification on the sale.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *