bazaarvoice / jolt

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

How to display output in specific order (kind of sort or set fields/objects in specific order) #1242

Closed mordfustang00 closed 3 months ago

mordfustang00 commented 4 months ago

Hello. I would like to ask how we can set output in specific order. Details are the following:

Below is my json input: { "head": { "csn": "CAR00000000000001", "txn": "1234567890ABCDEFGHIJKL", "date": "Mon 01 Jan 2024 00:00:01 GMT", "gmt": -3 }, "carStatusInfo": { "odo": { "unit": 3, "value": 2500.2 }, "carLoc": { "exactLoc": { "latitude": -22.951698577519057, "longitude": -43.21044431004139 }, "time": "20240101000001" }, "carInfo": { "info": { "Faixa": [ { "FaixaGas": { "ecarFaixa": { "value": 100 }, "fuelFaixa": { "value": 90 }, "absoluteFaixa": { "value": 80 } }, "class": 7 } ] }, "start": true, "tirePsi": { "FrontL": 0, "FrontR": 0, "BackL": 0, "BackR": 0, "All": 0 } } } }

where the specs are the following: [ { "operation": "shift", "spec": { "head": { "csn": "csn", "txn": "transactionId", "date": "statusTimeCode.date", "gmt": "statusTimeCode.gmt" }, "carStatusInfo": { "odo": { "unit": "odo.unit", "value": "odo.value" }, "carLoc": { "exactLoc": { "latitude": "exactLoc.Lat", "longitude": "exactLoc.Lon" }, "time": "locationTimeCode.date", "#-3": "locationTimeCode.gmt" }, "carInfo": { "info": { "Faixa": { "*": { "FaixaGas": { "ecarFaixa": { "value": "Faixa.ecarFaixa" }, "fuelFaixa": { "value": "Faixa.fuelFaixa" }, "absoluteFaixa": { "value": "Faixa.absoluteFaixa" } }, "class": "Faixa.unidade" } } }, "start": "ligar", "tirePsi": { "FrontL": "psi.FrenteLeft", "frontR": "psi.FrenteRight", "BackL": "psi.VoltarEsquerda", "BackR": "psi.VoltarCerta", "All": "psi.All" } } } } } ]

and this is the output: { "csn" : "CAR00000000000001", "transactionId" : "1234567890ABCDEFGHIJKL", "statusTimeCode" : { "date" : "Mon 01 Jan 2024 00:00:01 GMT", "gmt" : -3 }, "odo" : { "unit" : 3, "value" : 2500.2 }, "locationTimeCode" : { "gmt" : "-3", "date" : "20240101000001" }, "exactLoc" : { "Lat" : -22.951698577519057, "Lon" : -43.21044431004139 }, "Faixa" : { "ecarFaixa" : 100, "fuelFaixa" : 90, "absoluteFaixa" : 80, "unidade" : 7 }, "ligar" : true, "psi" : { "FrenteLeft" : 0, "VoltarEsquerda" : 0, "VoltarCerta" : 0, "All" : 0 } }

what i need to the output: { "csn": "CAR00000000000001", "transactionId": "1234567890ABCDEFGHIJKL", "psi": { "FrenteLeft": 0, "VoltarEsquerda": 0, "VoltarCerta": 0, "All": 0 }, "exactLoc": { "Lat": -22.951698577519057, "Lon": -43.21044431004139 }, "Faixa": { "ecarFaixa": 100, "fuelFaixa": 90, "absoluteFaixa": 80, "unidade": 7 }, "posição": [ { "attribute:": "odo", "value": "2500.2" }, { "attribute:": "ligar", "value": true } ], "statusTimeCode": { "date": "Mon 01 Jan 2024 00:00:01 GMT", "gmt": -3 }, "locationTimeCode": { "gmt": "-3", "date": "20240101000001" } }

Please advise how to achieve the output in specific order using available input json. Thanks.

mordfustang00 commented 4 months ago

apologies. i messed up with the input. it should be correct now when testing in jolt demo. what im trying to ask if we can sort depending on specific order of fields/object. i see that the sort will only sort alphabetically.

gbouget commented 4 months ago

Can you add this specification after yours?

[
  {
    "operation": "shift",
    "spec": {
      // your specification
  },
  {
    "operation": "shift",
    "spec": {
      "csn": "&",
      "transactionId": "&",
      "psi": "&",
      "exactLoc": "&",
      "Faixa": "&",
      "posição": "&",
      "statusTimeCode": "&",
      "locationTimeCode": "&"
    }
  }
]

image

mordfustang00 commented 4 months ago

thanks for this 💯