简体   繁体   English

将一种 JSON 格式转换为另一种 JSON 格式的 Java 脚本

[英]Java script to convert the one JSON format to another JSON format

I'm new to javascript, and still learning.我是 javascript 的新手,还在学习。

So, I have below input of json file所以,我输入了以下 json 文件

{
    "m_documents": {
        "documents": {
            "document": {
                "document": {
                    "id": [9286154, 9286163, 9306789, 9439618, 9513434, 9758081, 9866904, 9971257, 11229157, 11321316, 11323467, 11446058, 11481394, 11503220, 11522976, 11546733, 11571103, 11582740],
                    "created": ["2018-12-27", "2018-12-27", "2018-12-27", "2019-01-21", "2019-02-01", "2019-03-29", "2019-04-23", "2019-05-20", "2019-11-15", "2019-12-11", "2019-12-11", "2019-12-24", "2020-01-07", "2020-01-09", "2020-01-10", "2020-01-13", "2020-01-15", "2020-01-17"],
                    "name": ["H70", "H70", "H70", "H70", "H82", "H71", "H71", "H71", "H71", "H72", "H70", "H70", "H70", "H70", "H70", "H70", "H72", "H70"],
                    "type": ["H041", "H041", "H041", "H041", "H064", "H064", "H064", "H064", "H064", "H047", "H041", "H041", "H041", "H041", "H041", "H041", "H047", "H041"]
                },
                "case": {
                    "name": ["V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01", "V01"],
                    "work_type": {
                    "group": ["ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC"],
                    "id": ["1.2.4278465", "1.2.4278465", "1.2.4295143", "1.2.4363157", "1.2.4392970", "1.2.4494342", "1.2.4546187", "1.2.4592022", "1.2.5088384", "1.2.5150085", "1.2.5162736", "1.2.5236182", "1.2.5249537", "1.2.5269876", "1.2.5287418", "1.2.5305056", "1.2.5317864", "1.2.5327907"]
                    }
                }
            }
        }
    }
}

I would like to convert like this,我想这样转换,

   {
        "documents": {
            "document": [{"id": "9286154", "created": "2018-12-27", "name": "H70", "type": "H041"}],
            "case": [{"id": "V01" , "work_type": {"group": "ABC", "id": "1.2.4278465"}}]
        },
        "documents": {
            "document": [{"id": "9286163", "created": "2018-12-27", "name": "H70", "type": "H041"}],
            "case": [{"name": 0, "work_type": {"group": "ABC", "id": "1.2.4278465"}}]
        },
   "documents": {...}   
    }

How can I achieve this?我怎样才能做到这一点? I did like this, passing input to this function, but for some reason, case type is not working我确实喜欢这个,将输入传递给这个函数,但由于某种原因,案例类型不起作用

function Entries(entries) {
    var Result = [];
    if (entries.length) {
        var arrayMode = entries[0].value.length !== undefined;
        var itemCount = arrayMode ? entries[0].value.length : 1;
        for (var i = 0; i < itemCount; i++) {
            var zippedObject = {};
            for (var j = 0; j < entries.length; j++) {
                zippedObject[entries[j].key] = arrayMode ? entries[j].value[i] : entries[j].value;
            }
            Result.push(zippedObject);
        }
    }
    return Result;
}

This input data has been edited based on old response.此输入数据已根据旧响应进行编辑。 I hope this time the data and expected outcomes make sense.我希望这次的数据和预期结果有意义。 I know there must be nicer and shorter way to execute it.我知道必须有更好更短的方法来执行它。 Keys can be duplicated.钥匙可以复制。

This function should return what I believe you asked for (given the comments):这个函数应该返回我认为你要求的(给出评论):

 function ConvertJSON(input) { let output = []; let details = input['m_documents']['documents']['document']['document']; let cases = input['m_documents']['documents']['document']['case']; details['id'].map((id, i) => { let newJSON = { 'documents': { 'document': [{ 'id': id, 'created': details['created'][i], 'name': details['name'][i], 'type': details['type'][i] }], 'case': [{ 'name': cases['name'][i], 'work_type': { 'group': cases['work_type']['group'][i], 'id': cases['work_type']['id'][i] } }] } }; output.push(newJSON); }); return output; } var input = { 'm_documents': { 'documents': { 'document': { 'document': { 'id': [ 9286154, 9286163, 9306789, 9439618, 9513434, 9758081, 9866904, 9971257, 11229157, 11321316, 11323467, 11446058, 11481394, 11503220, 11522976, 11546733, 11571103, 11582740 ], 'created': [ '2018-12-27', '2018-12-27', '2018-12-27', '2019-01-21', '2019-02-01', '2019-03-29', '2019-04-23', '2019-05-20', '2019-11-15', '2019-12-11', '2019-12-11', '2019-12-24', '2020-01-07', '2020-01-09', '2020-01-10', '2020-01-13', '2020-01-15', '2020-01-17' ], 'name': ['H70', 'H70', 'H70', 'H70', 'H82', 'H71', 'H71', 'H71', 'H71', 'H72', 'H70', 'H70', 'H70', 'H70', 'H70', 'H70', 'H72', 'H70'], 'type': [ 'H041', 'H041', 'H041', 'H041', 'H064', 'H064', 'H064', 'H064', 'H064', 'H047', 'H041', 'H041', 'H041', 'H041', 'H041', 'H041', 'H047', 'H041' ] }, 'case': { 'name': ['V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01', 'V01'], 'work_type': { 'group': ['ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC'], 'id': [ '1.2.4278465', '1.2.4278465', '1.2.4295143', '1.2.4363157', '1.2.4392970', '1.2.4494342', '1.2.4546187', '1.2.4592022', '1.2.5088384', '1.2.5150085', '1.2.5162736', '1.2.5236182', '1.2.5249537', '1.2.5269876', '1.2.5287418', '1.2.5305056', '1.2.5317864', '1.2.5327907' ] } } } } } }; console.log(ConvertJSON(input));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM