nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.25k stars 6.57k forks source link

Cannot unflatten json object #4349

Open aka-mj opened 2 months ago

aka-mj commented 2 months ago

Description

I have a flattened object that I then want to unflatten but get the following error instead:

libc++abi: terminating with uncaught exception of type nlohmann::json_abi_v3_11_3::detail::type_error: [json.exception.type_error.315] values in object must be primitive

Reproduction steps

call unflatten() on a flatten json object.

Expected vs. actual results

{"SettingsRequest":{
    "ApplicationError":["1","No ACK","2","No ACK","3","No ACK","4","No ACK"],
    "DateAppliedOnDevice":"2024-04-15T09:29:25Z",
    "DateToExecuteOnDevice":"2022-08-04T17:01:39.645090001Z",
    "DeviceId":"12345",
    "SettingsPayloadId":34,
    "SettingsValues":null
}}   
libc++abi: terminating with uncaught exception of type nlohmann::json_abi_v3_11_3::detail::type_error: [json.exception.type_error.315] values in object must be primitive

Minimal code example

No response

Error messages

No response

Compiler and operating system

clang 13, linux

Library version

3.11.3

Validation

nlohmann commented 2 months ago

Your JSON value does not satisfy the requirements for unflatten, see https://json.nlohmann.me/api/basic_json/unflatten/

nlohmann commented 2 months ago

The flattened version of your JSON value would be

{
    "/SettingsRequest/ApplicationError/0": "1",
    "/SettingsRequest/ApplicationError/1": "No ACK",
    "/SettingsRequest/ApplicationError/2": "2",
    "/SettingsRequest/ApplicationError/3": "No ACK",
    "/SettingsRequest/ApplicationError/4": "3",
    "/SettingsRequest/ApplicationError/5": "No ACK",
    "/SettingsRequest/ApplicationError/6": "4",
    "/SettingsRequest/ApplicationError/7": "No ACK",
    "/SettingsRequest/DateAppliedOnDevice": "2024-04-15T09:29:25Z",
    "/SettingsRequest/DateToExecuteOnDevice": "2022-08-04T17:01:39.645090001Z",
    "/SettingsRequest/DeviceId": "12345",
    "/SettingsRequest/SettingsPayloadId": 34,
    "/SettingsRequest/SettingsValues": null
}

With this value, unflatten does work which yields

{
    "SettingsRequest": {
        "ApplicationError": [
            "1",
            "No ACK",
            "2",
            "No ACK",
            "3",
            "No ACK",
            "4",
            "No ACK"
        ],
        "DateAppliedOnDevice": "2024-04-15T09:29:25Z",
        "DateToExecuteOnDevice": "2022-08-04T17:01:39.645090001Z",
        "DeviceId": "12345",
        "SettingsPayloadId": 34,
        "SettingsValues": null
    }
}