Magesh
Magesh

Reputation: 427

Retain some json key value pairs while transforming them using jolt

I just started learning jolt. I want to transform a json to a desired format. I'm almost done but stuck with this point

My input json looks like

{ "first_name": {
    "label": "First name",
    "type": "text",
    "value": "John"
  },
  "last_name": {
    "label": "Last name",
    "type": "text",
    "value": "Doe"
  },
  "email": {
    "label": "Email",
    "type": "text",
    "value": "[email protected]"
  }
  "id": 123,
  "marital_status": "Single",
  "author_id": null,
  "company": null,
  "address": {
    "city": {
      "label": "city",
      "dom_type": "dropdown",
      "value": "test"
    },
    "state": {
      "label": "state",
      "dom_type": "dropdown",
      "value": "state"
    },
    "country": {
      "label": "country",
      "dom_type": "dropdown",
      "value": "country"
    }
  }
}

to an output format like this

{
 "first_name" : "John", "last_name" : "Doe", "email" : "[email protected]", 
 "id": 123, "marital_status": "Single", "author_id": null, "company": null,
  "address" : { "city" : "test", "state" : "test", "country" : "test" }
}

I have tried this shift spec

[
  {
    "operation": "shift",
    "spec": {
      "address": {
        "*": {
          "@value": "address.&1"
        }
      },
      "*": {
        "@value": "&1"
      }
    }
  }
]

And got

{
     "first_name" : "John", "last_name" : "Doe", "email" : "[email protected]", "address" : { "city" : "test", "state" : "test", "country" : "test" }
 }

Because the matcher "*" discards the simple key value pairs. I know that I'm missing something. Any help?

Upvotes: 0

Views: 899

Answers (1)

Milo S
Milo S

Reputation: 4586

Because the matcher "*" discards the simple key value pairs. -> It isn't discarding them, it is matching them, but not finding a sub-property of "value".

Your input data is fundamentally in 3 different formats

  1. Things below address
  2. Things that are singular in value like "id"
  3. Things that have nested data

The "*" just matches the Left-hand-side / key.

In this case you will need to explicitly list either the keys that are singular, or the keys that have nested data.

Spec

[
  {
    "operation": "shift",
    "spec": {
      "address": {
        "*": {
          "@value": "address.&1"
        }
      },
      "id|marital_status|author_id|company": "&",
      "*": {
        "@value": "&1"
      }
    }
  }
]

Upvotes: 1

Related Questions