0

I am using jolt to convert one json to another json but I am not getting correct output json for given input json

Input :

{  
  "user":{  
        "homePhone":"12345678901",
        "mobilePhone":"12346750983",
        "email":"test@example.com"
   }
}

Expected Json :

{  
   "contact":[  
    {  
      "class":"PhoneClass",
      "phoneNumber":"12345678901",
      "mobile":false,
      "preferred":false
   },
   {  
      "class":"PhoneClass",
      "phoneNumber":"12346750983",
      "mobile":true,
      "preferred":true
   },
   {  
      "class":"EmailClass",
      "email":"test@example.com"
   }
]
}

In output json there are some extra field also I need to add.

ppb
  • 2,299
  • 4
  • 43
  • 75

1 Answers1

0

How much of this is dynamic? If for your example you know you'll have those three input fields and want three output objects, this spec should work:

[
  {
    "operation": "shift",
    "spec": {
      "user": {
        "homePhone": "contact[0].phoneNumber",
        "mobilePhone": "contact[1].phoneNumber",
        "email": "contact[2].email"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "contact[]": {
        "0": {
          "class": "PhoneClass",
          "mobile": false,
          "preferred": false
        },
        "1": {
          "class": "PhoneClass",
          "mobile": true,
          "preferred": true
        },
        "2": {
          "class": "EmailClass"
        }
      }
    }
  }
]
mattyb
  • 11,693
  • 15
  • 20
  • Thank you mattyb for your response and it is working fine but what about phoneNumber and email property which I need into output json, How I can add this i specs. – ppb Apr 24 '18 at 17:21
  • Where do you want to add them? – mattyb Apr 24 '18 at 17:22
  • If you see Expected Json in my question, I want add them into contact array. – ppb Apr 24 '18 at 17:30
  • Using your input JSON and my spec, I get your Expected JSON out (although the fields are out of order). What are you getting? – mattyb Apr 24 '18 at 17:41
  • I am getting { `"contact" : [ { "class" : "PhoneClass", "mobile" : false, "preferred" : false }, { "preferred" : true, "class" : "PhoneClass", "mobile" : true }, { "class" : "EmailClass" } ] }` this output. – ppb Apr 24 '18 at 18:40
  • Are you using a Chain spec with both the Shift and Default specs inside (as they are above), or are you just applying the Default spec? – mattyb Apr 24 '18 at 18:46