Cbimport changes the order of attributes document

CB v 6.6.

When I use cbexport to files it lists Document JSON attributes in the order they exist in the database, e.g. (taken from export file):

{“insert_time”:1700574649,“last_update”:1700574649,“data”:{“list”:“black”},“imei”:“12345678901235”}

However, when I use cbimport for this file, the document JSON structure (order) has changed into:

{
“data”: {
“list”: “black”
},
“insert_time”: 1700574649,
“last_update”: 1700574649
}

Seems like upon import the attributes are re-ordered alphabetically.

Is there a way to prevent this from happening?

I don’t know if there is a way to prevent it. By definition, json attributes are unordered.

I wouldn’t expect the concern is with cbimport, but rather the SQL++ (presumably) retrieval ?

# echo '{"c":1,"b":2,"a":3}' > /tmp/t.json
# cbimport json --format lines -c http://localhost:8091 -b default -u Administrator -p pword -g 'example' -d file:///tmp/t.json
JSON `file:///tmp/t.json` imported to `http://localhost:8091` successfully
Documents imported: 1 Documents failed: 0
# cbc cat example -u Administrator -P pword -U couchbase://localhost/default
example              CAS=0x179a00116cd20000, Flags=0x2000006, Size=20, Datatype=0x01(JSON)
{"c":1,"b":2,"a":3}

i.e. the document in the database is as it was presented. (Should be the same via the “Documents” page in the UI.)

However:

$ curl -su Administrator:password http://localhost:8093/query/service -d 'metrics=false&signature=false&pretty=true&statement=select d.* from default d use keys["example"]'
{
    "requestID": "e4fae89c-6de5-472e-aab9-108917d08b89",
    "results": [
    {
        "a": 3,
        "b": 2,
        "c": 1
    }
    ],
    "status": "success"
}

SQL++ (before MB-53938) alphabetises projection element names (and after still does for * expansions and nested fields).

HTH.

[edit] I should add, that my response in no way contradicts @mreiche’s - JSON doesn’t enforce field order hence the server is free to order as it sees fit.[/edit]

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.