skip to Main Content

I am given the below json and it needs to be concatenated based on condition if it not null

{
  "id": "4583",
  "price": "149.99",
  "sale_price": ""
}

Can someone help me out here?

expected output

{
  "id": "4583",
  "price": "149.99 EUR",
  "sale_price": ""
}

In above json price was concatenated with EUR but sale_price is as it is because it is null.

I am using below jolt, it us updating sale_price as well.

{
    "operation": "modify-overwrite-beta",
    "spec": {
      "price": "=concat(@(1,price),' EUR')",
      "sale_price": ["=isNull", "=concat(@(1,sale_price),' EUR')"]
    }
  }

2

Answers


  1. I hope the following Jolt solution will be helpful to you.

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "price": "=concat(@(1,price),' EUR')",
          "sale_amount": "=concat(@(1,sale_price),' EUR')"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "sale_price": {
            "": {
              "@1": "sale_price"
            },
            "*": {
              "@(2,sale_amount)": "sale_price"
            }
          },
          "*": "&"
        }
      },
      {
        "operation": "remove",
        "spec": {
          "sale_amount": ""
        }
      }
    ]
    
    Login or Signup to reply.
  2. The empty value("") and the null value are different. So, we need to manipulate an empty value in order to convert to a null one, and consequently to be able to apply the isNull and the notNull functions such as

    [
      {//convert the empty values to `null` ones
        "operation": "shift",
        "spec": {
          "*": {
            "": { "@": "&2" },
            "*": { "@1": "&2" }
          }
        }
      },
      {//apply isNull and notNull functions consequtively
        "operation": "modify-overwrite-beta",
        "spec": {
          "*rice": ["=isNull", "=concat(@(1,&),' EUR')"]
          //Elvis Operator returns RHS(right-hand-side) whenever LHS is not null
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*rice": ["=notNull", ""]
          //Elvis Operator returns RHS whenever LHS is null
        }
      }
    ]
    
    • this link + (4 times) PgDn ->> Elvis
      Operator
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search