mi pregunta es muy confusa espero darme a entender correctamente,
tengo el siguiente Json extraido de un PDF, cabe mencionar que esta simplificado, solo inclui la una pagina por cuestiones de limites de caracteres, pero deseo buscar en todas las pags, la estructura es la siguiente
{
"meta": {
"info": {
"PDFFormatVersion": "1.4",
"Language": null,
"EncryptFilterName": null,
"IsLinearized": false,
"IsAcroFormPresent": false,
"IsXFAPresent": false,
"IsCollectionPresent": false,
"IsSignaturesPresent": false,
"Creator": "JasperReports (reporteSemanasCotizadas)",
"Producer": "iText 2.1.7 by 1T3XT",
"ModDate": "D:20220713094658-05'00'",
"CreationDate": "D:20220713094658-05'00'"
},
"metadata": null
},
"pages": [
{
"pageInfo": {
"num": 1,
"scale": 1,
"rotation": 0,
"offsetX": 0,
"offsetY": 0,
"width": 612,
"height": 792
},
"links": [],
"content": [
{
"x": 225.13,
"y": 108.78999999999996,
"str": "Instituto Mexicano del Seguro Social",
"dir": "ltr",
"width": 161.7399999999999,
"height": 10,
"fontName": "Helvetica"
},
{},
{
"x": 438.82,
"y": 210.27999999999997,
"str": "DD",
"dir": "ltr",
"width": 8.664,
"height": 6,
"fontName": "Helvetica"
},
{},
{
"x": 460.82800000000003,
"y": 210.27999999999997,
"str": "MM",
"dir": "ltr",
"width": 9.996,
"height": 6,
"fontName": "Helvetica"
},
{},
{
"x": 484.16800000000006,
"y": 210.27999999999997,
"str": "YYYY",
"dir": "ltr",
"width": 16.008,
"height": 6,
"fontName": "Helvetica"
},
{},
{
"x": 247.53,
"y": 282.04,
"str": "Tu detalle de semanas cotizadas",
"dir": "ltr",
"width": 116.94399999999999,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 201.57,
"y": 141.78999999999996,
"str": "Constancia de Semanas Cotizadas en el IMSS",
"dir": "ltr",
"width": 207.8699999999999,
"height": 10,
"fontName": "Helvetica"
},
{},
{
"x": 462.83,
"y": 245.03999999999996,
"str": "363",
"dir": "ltr",
"width": 13.344000000000001,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 47,
"y": 226.03999999999996,
"str": "NSS:",
"dir": "ltr",
"width": 18.672,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 47,
"y": 205.03999999999996,
"str": "JUAN PEREZ GOMEZ",
"dir": "ltr",
"width": 154.24800000000002,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 70,
"y": 226.03999999999996,
"str": "21139854785",
"dir": "ltr",
"width": 48.928000000000004,
"height": 8,
"fontName": "Helvetica"
}
]
},
{
"pageInfo": {
"num": 2,
"scale": 1,
"rotation": 0,
"offsetX": 0,
"offsetY": 0,
"width": 612,
"height": 792
},
"links": [],
"content": [
{
"x": 225.13,
"y": 108.78999999999996,
"str": "Instituto Mexicano del Seguro Social",
"dir": "ltr",
"width": 161.7399999999999,
"height": 10,
"fontName": "Helvetica"
},
{},
{
"x": 312,
"y": 216.95000000000005,
"str": "17/09/2021",
"dir": "ltr",
"width": 40.032000000000004,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 147,
"y": 196.95000000000005,
"str": "BAJA CALIFORNIA",
"dir": "ltr",
"width": 70.23999999999998,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 388,
"y": 216.95000000000005,
"str": "Salario Base de Cotización */",
"dir": "ltr",
"width": 103.59999999999998,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 44,
"y": 216.95000000000005,
"str": "Fecha de alta",
"dir": "ltr",
"width": 48.472000000000016,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 232,
"y": 216.95000000000005,
"str": "Fecha de baja",
"dir": "ltr",
"width": 50.695999999999984,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 147,
"y": 216.95000000000005,
"str": "20/01/2020",
"dir": "ltr",
"width": 40.032000000000004,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 44,
"y": 196.95000000000005,
"str": "Entidad federativa",
"dir": "ltr",
"width": 64.48,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 518.86,
"y": 216.95000000000005,
"str": "$ 620.67",
"dir": "ltr",
"width": 31.13600000000005,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 44,
"y": 156.95000000000005,
"str": "Nombre del patrón",
"dir": "ltr",
"width": 66.24800000000002,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 147,
"y": 156.95000000000005,
"str": "TCL MOKA MANUFACTURING",
"dir": "ltr",
"width": 113.33599999999998,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 44,
"y": 176.95000000000005,
"str": "Registro Patronal",
"dir": "ltr",
"width": 61.80000000000001,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 147,
"y": 176.95000000000005,
"str": "A086348210",
"dir": "ltr",
"width": 45.368,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 322.26,
"y": 252.03999999999996,
"str": "Fecha de movimiento",
"dir": "ltr",
"width": 76.47199999999998,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 129.6,
"y": 252.03999999999996,
"str": "Tipo de movimiento",
"dir": "ltr",
"width": 69.79999999999998,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 479.32,
"y": 252.03999999999996,
"str": "Salario Base",
"dir": "ltr",
"width": 45.352,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 340.48,
"y": 272.03999999999996,
"str": "17/09/2021",
"dir": "ltr",
"width": 40.032000000000004,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 154.5,
"y": 272.03999999999996,
"str": "BAJA",
"dir": "ltr",
"width": 20.008000000000003,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 486.43,
"y": 272.03999999999996,
"str": "$ 620.67",
"dir": "ltr",
"width": 31.135999999999992,
"height": 8,
"fontName": "Helvetica"
},
]
},
{
"pageInfo": {
"num": 3,
"scale": 1,
"rotation": 0,
"offsetX": 0,
"offsetY": 0,
"width": 612,
"height": 792
},
"links": [],
"content": [
{
"x": 225.13,
"y": 108.78999999999996,
"str": "Instituto Mexicano del Seguro Social",
"dir": "ltr",
"width": 161.7399999999999,
"height": 10,
"fontName": "Helvetica"
},
{},
{
"x": 322.26,
"y": 141.03999999999996,
"str": "Fecha de movimiento",
"dir": "ltr",
"width": 76.47199999999998,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 129.6,
"y": 141.03999999999996,
"str": "Tipo de movimiento",
"dir": "ltr",
"width": 69.79999999999998,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 479.32,
"y": 141.03999999999996,
"str": "Salario Base",
"dir": "ltr",
"width": 45.352,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 340.48,
"y": 161.03999999999996,
"str": "01/07/2019",
"dir": "ltr",
"width": 40.032000000000004,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 109.82,
"y": 161.03999999999996,
"str": "MODIFICACION DE SALARIO",
"dir": "ltr",
"width": 109.35199999999998,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 486.43,
"y": 161.03999999999996,
"str": "$ 325.18",
"dir": "ltr",
"width": 31.135999999999992,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 340.48,
"y": 181.03999999999996,
"str": "24/06/2019",
"dir": "ltr",
"width": 40.032000000000004,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 140.5,
"y": 181.03999999999996,
"str": "REINGRESO",
"dir": "ltr",
"width": 48.007999999999996,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 486.43,
"y": 181.03999999999996,
"str": "$ 324.74",
"dir": "ltr",
"width": 31.135999999999992,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 42,
"y": 201.03999999999996,
"str": "/* Valor del último salario base de cotización diario en pesos.",
"dir": "ltr",
"width": 215.64800000000008,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 312,
"y": 301.95,
"str": "26/06/2019",
"dir": "ltr",
"width": 40.032000000000004,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 147,
"y": 281.95,
"str": "BAJA CALIFORNIA",
"dir": "ltr",
"width": 70.23999999999998,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 388,
"y": 301.95,
"str": "Salario Base de Cotización */",
"dir": "ltr",
"width": 103.59999999999998,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 44,
"y": 301.95,
"str": "Fecha de alta",
"dir": "ltr",
"width": 48.472000000000016,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 232,
"y": 301.95,
"str": "Fecha de baja",
"dir": "ltr",
"width": 50.695999999999984,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 147,
"y": 301.95,
"str": "23/05/2018",
"dir": "ltr",
"width": 40.032000000000004,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 44,
"y": 281.95,
"str": "Entidad federativa",
"dir": "ltr",
"width": 64.48,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 518.86,
"y": 301.95,
"str": "$ 324.74",
"dir": "ltr",
"width": 31.13600000000005,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 44,
"y": 241.95000000000005,
"str": "Nombre del patrón",
"dir": "ltr",
"width": 66.24800000000002,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 147,
"y": 241.95000000000005,
"str": "NOMINA INTEGRAL DE SOLUCIONES PRODUCTIVAS",
"dir": "ltr",
"width": 202.25600000000006,
"height": 8,
"fontName": "Helvetica"
},
{
"x": 44,
"y": 261.95000000000005,
"str": "Registro Patronal",
"dir": "ltr",
"width": 61.80000000000001,
"height": 8,
"fontName": "Helvetica"
},
{},
{
"x": 147,
"y": 261.95000000000005,
"str": "Z312983510",
"dir": "ltr",
"width": 44.92,
"height": 8,
"fontName": "Helvetica"
}
]
}
],
"filename": "./Employees/123.pdf"
}
El Json contiene un historial laboral mediante tablas que siempre tienen los mismos encabezados y que siempre tiene la misma estructura pero puede cambiar su tamaño dependiendo de los movimientos del usuario consultado en el reporte, el JSON contiene las coordenadas de cada string en X,Y, deseo crear un loop con todos los empleos que el usuario tuvo, debido a que el numero de empleos por cada usuario varia, no es correcto extraer el texto mediante sus coordenadas, puesto que dependiendo del numero de empleos que tiene un usuario las coordenadas variaran.
Para intentar solucionarlo tengo la siguiente funcion que consulte en este link busca un valor como el encabezado de la tabla en el JSON y extrae el valor anterior al localizado:
function searchByPrev(pages, txt) {
/*// Init txt
txt = txt.toLowerCase();*/
// Loop in all pages
for (let i = 0; i < pages.length; i++)
// Loop in all content
for (let j = 0; j < pages[i].content.length; j++)
// Test text and if match return next content
// (If you write j-1, you can have searchByNext function)
if (pages[i].content[j].str == txt && pages[i].content[j + 1])
// Return result
return pages[i].content[j + 1].str;
// No results found
return 'NotFound';
}
El problema que me encuentro al usar esta funcion es que el JSON se imprime desordenado y en ocasiones tengo dos valores buscados uno seguido del otro, los valores que busco pertenecen a los encabezados siguientes, es el bloque por cada uno de los empleos:
El codigo que imprime la respuesta en la consola es el siguiente:
//almacenar en variable
let result = JSON.stringify({
nombre: searchByPosition(reporte.pages, 47, 205),
curp: searchByPosition(reporte.pages, 75, 246),
nss: searchByPosition(reporte.pages, 70, 226),
semanasCotizadas: {
semanasCotizadas: searchByPosition(reporte.pages, 123, 322),
semanasReintegradas: searchByPosition(reporte.pages, 303, 322),
semanasDescontadas: searchByPosition(reporte.pages, 479, 322),
},
historialLaboral: [historial]
}, null, "\t");
// imprimir el resultado en la consola
console.log(result);
He creado un elemento que contendra el loop de los empleos y lo he agregado al codigo de arriba mediante la variable historial, se que esta mal, es solo un ejemplo, soy muy nuevo en Javascript y no se me ocurre una manera de obtener cada empleo en algo tan dinamico:
let historial = [reporte]
.forEach((historial) => {
{
fechaAlta = searchByPrev(historial.pages, 'Fecha de baja')
fechaBaja = searchByPrev(reporte.pages, ''),
salarioBaseCotizacion = searchByPrev(historial.pages, 'Entidad federativa'),
entidadFederativa = searchByPrev(reporte.pages, ''),
nombrePatron = searchByPrev(historial.pages, 'Nombre del patrón'),
registroPatronal = searchByPrev(historial.pages, 'Registro Patronal')
movimientos =
{
"tipo": 'BAJA',
"fechaMovimiento": '17/09/2021',
"salarioBase": '$ 620.67'
},
{
"tipo": "MODIFICACION DE SALARIO",
"fechaMovimiento": '01/09/2021',
"salarioBase": '$ 620.67'
}
}
});
Esta es la respuesta que actualmente me arroja la consola:
{
nombre: 'HERNANDEZ LOPEZ JUAN',
curp: 'HEHC960204HBMHR08',
nss: '2113254845',
semanasCotizadas: {
semanasCotizadas: '363',
semanasReintegradas: '0',
semanasDescontadas: '0'
},
historialLaboral: [ undefined ]
}
Y asi es como lo necesito:
{
"nombre": "HERNANDEZ LOPEZ JUAN",
"curp": "HEHC960204HBMHR08",
"nss": "2113254845",
"semanasCotizadas": {
"semanasCotizadas": "363",
"semanasReintegradas": "0",
"semanasDescontadas": "0"
},
"historialLaboral": [
{
"fechaAlta": "01/10/2020",
"fechaBaja": "Vigente",
"salarioBaseCotizacion": "$148.1",
"entidadFederativa": "NUEVO LEÓN",
"nombrePatron": "VERTICAL CONNECTION SA DE CV",
"registroPatronal": "Y393262110"
},
{
"fechaAlta": "23/09/2020",
"fechaBaja": "08/10/2020",
"salarioBaseCotizacion": "$156.78",
"entidadFederativa": "NUEVO LEÓN",
"nombrePatron": "ROSTA CONSULTORES Y ASESORES SA DE CV",
"registroPatronal": "Y404318210"
},
{
"fechaAlta": "16/02/2019",
"fechaBaja": "16/10/2020",
"salarioBaseCotizacion": "$139.36",
"entidadFederativa": "NUEVO LEÓN",
"nombrePatron": "COMERCIALIZADORA SLS",
"registroPatronal": "Y392535210"
},
],
"estatus": "OK",
"claveMensaje": "0"
}
El problema que me encuentro al usar esta función es que el JSON se imprime desordenado
Por favor agrega el JSON que se imprime por consola para poder entenderte.