bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

JOLT Spec Shift - ID Auto Increment #1203

Open deguedess opened 1 year ago

deguedess commented 1 year ago

Hi Guys, I'm trying to add an auto-increment ID for each product in this input: { "xml": { "body": [ { "ticket": "03024026", "status": "Agendado", "agendado": "2023-06-26T10:00:00.000Z", "obs": "OV 47980 GASOLINA", "carga_cescarga": "Carga", "operacao": "Itabuna", "tipo_de_veiculo": "Bottom", "tipo_de_carga": "Venda Mercado Interno", "tipo_de_produto": "-Manual", "peso_ref": 10000, "produtos": [ { "codigo": "PB62A", "descricao": "GASOLINA C", "peso": "2000.0", "unitCode": "L20" }, { "codigo": "PB62A", "descricao": "GASOLINA C", "peso": "3000.0", "unitCode": "L20" }, { "codigo": "PB62A", "descricao": "GASOLINA C", "peso": "5000.0", "unitCode": "L20" } ], "recursos": [ "Baia 01 IT", "Baias Claros e Biocomb", "Itabuna" ], "condicao_pagamento": "Z001 - 1 dia de prazo", "transportadora": { "id": "111222", "nome": "TRANSPORTES" }, "cliente": { "id": "33233", "nome": "DE - ITABUNA" }, "motorista": { "cpf": "606", "cnh": "001", "nome": "ALVES" }, "n_compartimentos": 3, "compartimentos": [ "62A - Gasolina C", "62A - Gasolina C", "62A - Gasolina C" ], "id": 1 }, { "ticket": "04024089", "status": "Agendado", "agendado": "2023-06-26T13:00:00.000Z", "obs": "47983 GASOLINA \n47984 S10", "carga_cescarga": "Carga", "operacao": "Jequié", "tipo_de_veiculo": "Bottom", "tipo_de_carga": "Venda Mercado Interno", "tipo_de_produto": "-Manual", "peso_ref": 15000, "produtos": [ { "codigo": "PB6DK", "descricao": "DIESEL B S10", "peso": "5000.0", "unitCode": "L20" }, { "codigo": "PB62A", "descricao": "GASOLINA C", "peso": "10000.0", "unitCode": "L20" } ], "recursos": [ "Baia 03 JE", "Baias Claros e Biocomb", "Jequié" ], "condicao_pagamento": "Z000 - ANTECIPADO", "transportadora": { "id": "12345", "nome": "CIA LTDA" }, "cliente": { "id": "133", "nome": "JEQUIE" }, "motorista": { "cpf": "986543", "cnh": "774", "nome": "GUILHER" }, "n_compartimentos": 3, "compartimentos": [ "6DK - Oleo Diesel B S10", "62A - Gasolina C", "62A - Gasolina C" ], "id": 2 } ] } }

Here is my JOLT

[ { "operation": "shift", "spec": { "xml": { "body": { "*": { "*": "&", "produtos": { "*": { "$": "produtos[#2].id", "*": "produtos[#2].&" } } } } } } }, { "operation": "modify-overwrite-beta", "spec": { "produtos": { "*": { "id": "=intSum(@(1,id),1)" } } } } ]

And here is my OUTPUT

{ "ticket" : [ "03024026", "04024089" ], "status" : [ "Agendado", "Agendado" ], "agendado" : [ "2023-06-26T10:00:00.000Z", "2023-06-26T13:00:00.000Z" ], "obs" : [ "OV 47980 GASOLINA", "47983 GASOLINA \n47984 S10" ], "carga_cescarga" : [ "Carga", "Carga" ], "operacao" : [ "Itabuna", "Jequié" ], "tipo_de_veiculo" : [ "Bottom", "Bottom" ], "tipo_de_carga" : [ "Venda Mercado Interno", "Venda Mercado Interno" ], "tipo_de_produto" : [ "-Manual", "-Manual" ], "peso_ref" : [ 10000, 15000 ], "produtos" : [ { "id" : 1, "codigo" : [ "PB62A", "PB6DK" ], "descricao" : [ "GASOLINA C", "DIESEL B S10" ], "peso" : [ "2000.0", "5000.0" ], "unitCode" : [ "L20", "L20" ] }, { "id" : 1, "codigo" : [ "PB62A", "PB62A" ], "descricao" : [ "GASOLINA C", "GASOLINA C" ], "peso" : [ "3000.0", "10000.0" ], "unitCode" : [ "L20", "L20" ] }, { "id" : 3, "codigo" : "PB62A", "descricao" : "GASOLINA C", "peso" : "5000.0", "unitCode" : "L20" } ], "recursos" : [ "Baia 01 IT", "Baias Claros e Biocomb", "Itabuna", [ "Baia 03 JE", "Baias Claros e Biocomb", "Jequié" ] ], "condicao_pagamento" : [ "Z001 - 1 dia de prazo", "Z000 - ANTECIPADO" ], "transportadora" : [ { "id" : "111222", "nome" : "TRANSPORTES" }, { "id" : "12345", "nome" : "CIA LTDA" } ], "cliente" : [ { "id" : "33233", "nome" : "DE - ITABUNA" }, { "id" : "133", "nome" : "JEQUIE" } ], "motorista" : [ { "cpf" : "606", "cnh" : "001", "nome" : "ALVES" }, { "cpf" : "986543", "cnh" : "774", "nome" : "GUILHER" } ], "n_compartimentos" : [ 3, 3 ], "compartimentos" : [ "62A - Gasolina C", "62A - Gasolina C", "62A - Gasolina C", [ "6DK - Oleo Diesel B S10", "62A - Gasolina C", "62A - Gasolina C" ] ], "id" : [ 1, 2 ] } image

NOTE that the first and second groups of products have the same ID (1).

I have tried a lot of changes and none worked so far. Do you have any idea what I'm doing wrong? Thanks