Создание одного json из объединенного списка json

Я получаю несколько файлов json в цикле for, и каждый файл json в каждом цикле имеет формат:

{
    "myproperties": {
        "http.port": "8088",
        "http.base": "/abc",
        "http.path": "test"
    },
    "information": [{
        "abc": {
            "key1": "ghghghghgh"
        },
        "efg": {
            "key1": "value1"
        }
    }]
}

а также

{
    "myproperties": {
        "http.port": "6789",
        "db.base": "tat",
        "db.path": "ghghghg"
    },
    "information": [{
        "efg": {
            "key1": "ghghghghgh"
        },
        "ijk": {
            "key1": "value1"
        }
    }]
}

и так далее ……….

Мне удалось объединить все json-файлы в списке вне цикла for, и объединить json-список выглядит так:

    [{
    "myproperties": {
        "http.port": "8088",
        "http.base": "/abc",
        "http.path": "test"
    },
    "information": [{
        "abc": {
            "key1": "ghghghghgh"
        },
        "efg": {
            "key1": "value1"
        }
    }]
}, 
 {
    "myproperties": {
        "http.port": "6789",
        "db.base": "tat",
        "db.path": "ghghghg"
    },
    "information": [{
        "efg": {
            "key1": "ghghghghgh"
        },
        "ijk": {
            "key1": "value1"
        }
    }]
}]

Теперь я хочу сделать **single** json вывод из этого объединения json чего-то в следующем формате:

 {
    "myproperties": {
        "http.port": "6789",
        "db.base": "tat",
        "db.path": "ghghghg",
        "http.base": "/abc",
        "http.path": "test"
    },
    "information": [{
            "efg": {
                "key1": "ghghghghgh"
            },
            "ijk": {
                "key1": "value1"
            }
        },
        {
            "abc": {
                "key1": "ghghghghgh"
            },
            "efg": {
                "key1": "value1"
            }
        }
    ]
 }

обратите внимание, что в разделе myproperties есть только уникальный и отдельный узел. Я не уверен, как начать это с dataweave ... любой указатель будет оценен,

Я пробовал следующее:

%dw 1.0
%output application/json skipNullOn="everywhere",encoding="UTF-8"
---
payload map {

    myproperties: $.myproperties,
    information: $.information

}

Но не работает

Спасибо


person Rajesh Kumar    schedule 05.12.2018    source источник


Ответы (1)


Попробуйте mapObject для объединения свойств и сглаживания для изменения массива информации на один массив, например

%dw 1.0
%output application/json
---
{
    myproperties : payload.myproperties mapObject $ ,
    information : (flatten payload.information) 
}

HTH

Обновление: - Для различных свойств вы должны сначала получить отдельные свойства, а затем сопоставить все отдельные свойства. Но вы можете потерять повторяющиеся свойства. см. ниже

%dw 1.0
%output application/json
%var distinctProperties =((payload.myproperties mapObject $) pluck $$)  distinctBy $ 
%var aggregatedPropertiesMap =  (payload.myproperties mapObject $)
---
{
    myproperties : {( distinctProperties map { // interate over distinct properties 
        ($) : aggregatedPropertiesMap[$]    // map property as key and fetch its value from aggregatedPropertiesMap
    })} ,
    information : (flatten payload.information) 
}
person AnupamBhusari    schedule 05.12.2018
comment
Спасибо .. но как получить только уникальные поля myproperties ... например, если узел http.port присутствует во втором json, он не должен повторяться ... должен прийти только один http.port - person Rajesh Kumar; 06.12.2018