Transform one json into another with JavaScript

-2

I have a json returned with the Google Vision API that is the result of the upload of 3 different images, but in the same upload ...

    [
  {
    "errorMessage": "",
    "errorStatus": false,
    "fileMimeType": "image/jpeg",
    "fileName": "19091_047031795420170519212400126191-1-3.jpg",
    "meanConfidence": 100,
    "milliSecondsEmployed": 206650,
    "ocrInUse": true,
    "pageCount": 1,
    "pageDataList": [
      {
        "height": 2631,
        "pageNumber": 1,
        "rawText": "texto completo",
        "textSegments": [
          {
            "geometry": {
                "x": 1694.25,
                "y": -57
            },
            "orientation": 0,
            "text": "texto"
          }
        ],
        "width": 1860
      }
    ],
    "pending": false
  },
  {
    "errorMessage": "",
    "errorStatus": false,
    "fileMimeType": "image/jpeg",
    "fileName": "19091_047031795420170519212400126191-1-3.jpg",
    "meanConfidence": 100,
    "milliSecondsEmployed": 206654,
    "ocrInUse": true,
    "pageCount": 1,
    "pageDataList": [
      {
        "height": 2631,
        "pageNumber": 1,
        "rawText": "texto completo",
        "textSegments": [
          {
            "geometry": {
                "x": 594.75,
                "y": -134.25
            },
            "orientation": 0,
            "text": "texto"
          }
        ],
        "width": 1860
      }
    ],
    "pending": false
  },
  {
    "errorMessage": "",
    "errorStatus": false,
    "fileMimeType": "image/jpeg",
    "fileName": "19091_047031795420170519212400126191-1-2.jpg",
    "meanConfidence": 100,
    "milliSecondsEmployed": 238255,
    "ocrInUse": true,
    "pageCount": 1,
    "pageDataList": [
      {
        "height": 2631,
        "pageNumber": 1,
        "rawText": "texto completo",
        "textSegments": [
          {
            "geometry": {
                "x": 101.25,
                "y": -69.75
            },
            "orientation": 0,
            "text": "texto"
          }
        ],
        "width": 1860
      }
    ],
    "pending": false
  }
]

I have greatly shortened its format for this example ... what I want is that the pages are added to a new json, so that they are grouped as in this other json:

    [
  {
    "errorMessage": "",
    "errorStatus": false,
    "fileMimeType": "application/pdf",
    "fileName": "19091_047031795420170519212400126191-1.pdf",
    "meanConfidence": 100,
    "milliSecondsEmployed": 4750,
    "ocrInUse": false,
    "pageCount": 2,
    "pageDataList": [
      {
        "height": 841.8898,
        "pageNumber": 1,
        "rawText": "texto completo",
        "textSegments": [
          {
            "geometry": {
                "x": 368.24878,
                "y": -58.73446
            },
            "orientation": 0,
            "text": "texto"
          }]
      },
      {
        "height": 841.8898,
        "pageNumber": 2,
        "rawText": "texto completo",
        "textSegments": [
          {
            "geometry": {
                "x": 47.452,
                "y": -73.23697
            },
            "orientation": 0,
            "text": "texto"
          }]
      }
    ],
    "pending": false
  }
]

That is, that all pages go inside pageDataList, not that a different page is created for each page. How can I change from one type of json to the other?

    
asked by Norak 30.10.2017 в 15:42
source

2 answers

1

You can create a function that takes the values stored in the pageDataList property from the second element of your array and appends it to the same property but the first element, so that those values are only in that property in the first element and you can get rid of the others without losing that data. I propose to try the following, it works for me but you must verify that it solves your punctual problem.

//Creo una función para reformar mi JSON
var normalizar = function(obj){
  //Recorro el arreglo del JSON desde la segunda posición ya que tomare sus valores para ponerlos en el primer elemento
  for(var i=1;i<obj.length;i++){
    //Si tiene la propiedad "pageDataList"
    if(obj[i].pageDataList !== undefined){
      //Añado el dato almacenado en pageDataList a la propiedad con el mismo nombre del primer elemento
      obj[0].pageDataList.push(obj[i].pageDataList);
      //Elimino la propiedad en el elemento actual de la iteración
      delete obj[i].pageDataList;
    }
  }
  //Elimino los elementos del arreglo excepto el primero ya que su informacion ya se pasó hacia el arreglo de la propiedad
  //"pageDataList"
  for(var j=1;j<obj.length;j++){
    delete obj[j];
  }
};

//Dejo los datos con la estructura nueva
normalizar(data);

//Muestro la nueva estructura
console.log(data);

I'm clear that I used the same JSON that you included in your question

    
answered by 30.10.2017 / 16:16
source
1

Taking the information you provide as a reference, I want to assume that you respond to that information in a text string which will have to be manipulated as a json object for practicality.

This can be solved in a practical way:

JSON.parse(jsonString);

Let's say we have the answer in a variable called payload. To obtain the Json that you require, we have to create a new object which I suppose will be an object to generate a pdf. Therefore I propose the following solution using lodash , which is a highly recommended library to manage these details:

var payload = [
    {
        "errorMessage": "",
        "errorStatus": false,
        "fileMimeType": "image/jpeg",
        "fileName": "19091_047031795420170519212400126191-1-3.jpg",
        "meanConfidence": 100,
        "milliSecondsEmployed": 206650,
        "ocrInUse": true,
        "pageCount": 1,
        "pageDataList": [
            {
                "height": 2631,
                "pageNumber": 1,
                "rawText": "texto completo",
                "textSegments": [
                    {
                        "geometry": {
                                "x": 1694.25,
                                "y": -57
                        },
                        "orientation": 0,
                        "text": "texto"
                    }
                ],
                "width": 1860
            }
        ],
        "pending": false
    },
    {
        "errorMessage": "",
        "errorStatus": false,
        "fileMimeType": "image/jpeg",
        "fileName": "19091_047031795420170519212400126191-1-3.jpg",
        "meanConfidence": 100,
        "milliSecondsEmployed": 206654,
        "ocrInUse": true,
        "pageCount": 1,
        "pageDataList": [
            {
                "height": 2631,
                "pageNumber": 1,
                "rawText": "texto completo",
                "textSegments": [
                    {
                        "geometry": {
                                "x": 594.75,
                                "y": -134.25
                        },
                        "orientation": 0,
                        "text": "texto"
                    }
                ],
                "width": 1860
            }
        ],
        "pending": false
    },
    {
        "errorMessage": "",
        "errorStatus": false,
        "fileMimeType": "image/jpeg",
        "fileName": "19091_047031795420170519212400126191-1-2.jpg",
        "meanConfidence": 100,
        "milliSecondsEmployed": 238255,
        "ocrInUse": true,
        "pageCount": 1,
        "pageDataList": [
            {
                "height": 2631,
                "pageNumber": 1,
                "rawText": "texto completo",
                "textSegments": [
                    {
                        "geometry": {
                                "x": 101.25,
                                "y": -69.75
                        },
                        "orientation": 0,
                        "text": "texto"
                    }
                ],
                "width": 1860
            }
        ],
        "pending": false
    }
];

Now to create the pdf object conveniently we create an object in javascript and to generate its json we simply use the benefits of javascript.

var pdf = {
    "errorMessage": "",
    "errorStatus": false,
    "fileMimeType": "application/pdf",
    "fileName": "19091_047031795420170519212400126191-1.pdf",
    "meanConfidence": 100,
    "milliSecondsEmployed": 4750,
    "ocrInUse": false,
    "pageCount": 2,
    "pageDataList": _.chain(payload)
    .map(function(image) {
        pageDataList = _.get(image, 'pageDataList');
        if(! _.isUndefined(pageDataList)) {
            return _.first(pageDataList);
        }
    })
    .value(),
};

JSON.stringify(pdf);
    
answered by 30.10.2017 в 16:40