Home > Blockchain >  adding default values to an array in JOLT
adding default values to an array in JOLT

Time:05-11

I need to transform request to a SOLR microservice. Among many other attributes I have a list of fields to be returned from SOLR. I do need to add a couple of default fields there. I.e. with the following input:

{
  "jsonToTransform": {
    "authorizedCountries": [],
    "searchOptions": {
      "maxRecords": "10",
      "matchScore": "true",
      "matchIndicators": "true",
    },
    "searchAttrList": {
      "distanceUnit": "M",
      "phoneNumber": "369283729375",
      "distance": "8.5",
      "latitude": 44.71663,
      "longitude": 10.23196
    },
    "responseAttrList": ["distance","locationAddressLongitude","locationAddressLatitude","name","streetAddress","city","state","postalCode","countryCode","primaryCategoryCode","categoryCodeDesc","url"]
  }
}

I need to get this output:

{
  "searchOptions" : {
    "maxRecords" : "10",
    "matchScore" : "true",
    "matchIndicators" : "true"
  },
  "searchAttrList" : {
    "distance" : "13.68",
    "point" : "44.71663,10.23196"
  },
  "solrOptions" : {
    "responseElementsList" : ["distance","locationAddressLongitude","locationAddressLatitude","name","streetAddress","city","state","postalCode","countryCode","primaryCategoryCode","categoryCodeDesc","url","compatibilityIndex","privacyIndicator"],
    "countryCodeList" : [ ],
    "pnm_boost" : "1.0",
    "anm_boost" : "1.0",
    ...
  }
}

I have almost everything working, except adding these two fields at the end of responseElementsList. I have this spec:

{
  "operation": "default",
  "spec": {
    "*": {
      "solrOptions": {
        "anm_boost": "1.0",
        "pnm_boost": "1.0",
        "responseElementsList": ["compatibilityIndex","privacyIndicator"]
      }
    }
  }
},
{
  "operation": "shift",
  "spec": {
    "*": {
      "solr*": {
        "*": "&1.&",
        "@(1,responseAttrList)": "&1.responseElementsList",
        "@(1,authorizedCountries)": "&1.countryCodeList",
        ...
      },
      "search*": "&",
      "searchOptions": {
        "*": "&1.&",
        ...
      }
    }
  }
}

But in response I am getting :

"responseElementsList" : ["distance","locationAddressLongitude","locationAddressLatitude","name","streetAddress","city","state","postalCode","countryCode","primaryCategoryCode","categoryCodeDesc","url",["compatibilityIndex","privacyIndicator"]],

I did try some other variants but they did not produce desired output either. Please do tell me what I am doing wrong.

CodePudding user response:

You can add responseElementsList object to the shift transformation in which walk for the indices of the array, but not the whole array such as

[
 ....,
  {
    "operation": "shift",
    "spec": {
      "*": {
        "solr*": {
          "*": "&1.&",
          "@(1,responseAttrList)": "&1.responseElementsList",
          "responseElementsList": {
            "*": {
              "@": "&3.&2"
            }
          }, 
          "@(1,authorizedCountries)": "&1.countryCodeList",
        ...
      },
      "search*": "&",
      "searchOptions": {
        "*": "&1.&",
        ...
      }
    }
   }
  }
  ...
]

where &3 represents the key name "solrOptions" in order to combine under the common object, and &2 represents "responseElementsList" key name.

  • Related