-1

I'm new to javascript, and still learning.

So, I have below input of json file

{
    "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.

change198
  • 1,647
  • 3
  • 21
  • 60
  • 1
    What have you tried so far? – evolutionxbox Jan 21 '20 at 10:01
  • 3
    your expected response seems wrong. You're trying to have multiple values in an object with same key? Seems like you want an Array instead? – Vishal Sharma Jan 21 '20 at 10:12
  • Are you sure you want to have [duplicate keys](https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object) in your JSON? "documents" is repeated multiple times, as @VishalSharma said, I would suggest an array – CanobbioE Jan 21 '20 at 10:15
  • @VishalSharma , yes keys can be duplicate, I think you are right to start with array, can you suggest how to do this? – change198 Jan 21 '20 at 10:18
  • So @change198 you want list/array of documents and not just object. you need something like `[{"documents":{}},{"documents":{}}]` and not `{"documents":{},"documents":{}}` as this is not possible due to duplicate keys. – Samarth Jan 21 '20 at 10:46
  • @Samarth ok...thanks for clarification. Is it possible to do it? – change198 Jan 21 '20 at 12:22

1 Answers1

1

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));
CanobbioE
  • 222
  • 2
  • 11