bazaarvoice / jolt

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

Hi I need some help with the Transformation of New Output JSON can anyone help me on this #944

Open kirannarala opened 4 years ago

kirannarala commented 4 years ago

I Have some input JSON from upstream as below

{
  "status": "Success",
  "tracking_tag": "d56226bd42b28ed11122b8828c0a383b",
  "reco_count": 20,
  "reco": [
    {
      "yami_flg": 0,
      "img": "http://google.com/image",
      "no_image_flg": 0,
      "rms_shop_id": 213310,
      "review_score_average": "4.83",
      "review_count": "30",
      "stock_status": 3,
      "adult_flg": 0,
      "title": "テストサンプルテキスト",
      "rms_item_id": 19931734,
      "rms_item_no": "16220456",
      "note": "Nintendo Switch",
      "score": "0.15013999939",
      "id": "4902370545319",
      "genre_id": "566404"
    },
    {
      "yami_flg": 0,
      "img": "http://google.com/image",
      "no_image_flg": 0,
      "rms_shop_id": 213310,
      "review_score_average": "4.97",
      "review_count": "73",
      "stock_status": 10,
      "adult_flg": 0,
      "title": "D.D. / Imitation Rain (初回盤 CD+DVD)",
      "rms_item_id": 19822459,
      "rms_item_no": "16107614",
      "note": "Snow Man vs SixTONES",
      "score": "0.0963300037384",
      "id": "4988064946662",
      "genre_id": "101320"
    }
  ],
  "title": null
}

And i want the transformation of JSON to below output JSON

{
            "title": "テストサンプルテキスト",
            "serviceName": "テストサンプルテキスト",
            "serviceIcon": "Some Service Icon",
            "serviceHistoryImage": "Some Image",
            "serviceLink": {
                "webUrl": "https://google.com/",
                "appLinkAndroid": null,
                "appLinkIos": null
            },
            "items": [
                {
                    "title": "テストサンプルテキスト",
                    "subtitle": "Nintendo Switch",
                    "imageUrl": "http://google.com/image",
                    "link": {
                        "webUrl": "https://service.url/16107614?rtg=d56226bd42b28ed11122b8828c0a383b",
                        "appLinkAndroid": null,
                        "appLinkIos": null
                    }
                },
                {
                    "title": "D.D. / Imitation Rain (初回盤 CD+DVD)",
                    "subtitle": "Snow Man vs SixTONES",
                    "imageUrl": "http://google.com/image",
                    "link": {
                        "webUrl": "https://service.url/16220456?rtg=d56226bd42b28ed11122b8828c0a383b",
                        "appLinkAndroid": null,
                        "appLinkIos": null
                    }
                }
            ],
            "itemType": "content_feed"
        }

and below is the Spec which i wrote by understanding the Jolt documentation

[
  {
    "operation": "remove",
    "spec": {
      "reco": {
        "*": {
          "yami_flg": "",
          "no_image_flg": "",
          "rms_shop_id": "",
          "review_score_average": "",
          "review_count": "",
          "stock_status": "",
          "adult_flg": "",
          "rms_item_id": "",
          "score": "",
          "id": "",
          "genre_id": ""
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "tracking_tag": "TrackingTag",
      "reco": {
        "*": {
          "title": "=concat(@(1,title),'')",
          "subtitle": "=concat(@(1,note))",
          "imageUrl": "=concat(@(1,img))",
          "link": {
            "webUrl": "=concat('https://books.rakuten.co.jp/rb/',@(2,rms_item_no),'&rtg=',@(2,tracking_tag))"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "reco": "items"
    }
  },
  {
    "operation": "default",
    "spec": {
      "title": "テストサンプルテキスト",
      "serviceName": "テストサンプルテキスト",
      "serviceIcon": "Some Service Icon",
      "serviceHistoryImage": "Some Image",
      "serviceLink": {
        "webUrl": "https://google.com/",
        "appLinkAndroid": "",
        "appLinkIos": ""
      }
    }
 }
]

and can you provide some more documentation for how to traverse back to tree and set the values to items current node. For example in the above given input sample i have Tracking_tag which i need to add at the end of the Url as query parameter And what is the role of &1 and @1. Thanks In Advance, Kiran Kumar

zeshuai007 commented 4 years ago

I just made some changes.

[
  {
    "operation": "remove",
    "spec": {
      "reco": {
        "*": {
          "yami_flg": "",
          "no_image_flg": "",
          "rms_shop_id": "",
          "review_score_average": "",
          "review_count": "",
          "stock_status": "",
          "adult_flg": "",
          "rms_item_id": "",
          "score": "",
          "id": "",
          "genre_id": ""
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "reco": {
        "*": {
          "link": {
            "webUrl": "=concat('https://books.rakuten.co.jp/rb/',@(2,rms_item_no),'&rtg=',@(4,tracking_tag))",
            "appLinkAndroid": null,
            "appLinkIos": null
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "reco":{
        "*":{
            "title":"items.[&1].title",
            "note":"items.[&1].subtitle",
            "img":"items.[&1].imageUrl",
            "link":"items.[&1].link"
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "title": "テストサンプルテキスト",
      "serviceName": "テストサンプルテキスト",
      "serviceIcon": "Some Service Icon",
      "serviceHistoryImage": "Some Image",
      "serviceLink": {
        "webUrl": "https://google.com/",
        "appLinkAndroid": "",
        "appLinkIos": ""
      }
    }
 }
]
zeshuai007 commented 4 years ago

Hi @kirannarala . The error was judged at the position of 'tracking_tag', it is not at the same level as "rms_item_no", you can refer to my example above. Actually, I want to say that you are doing well.

kirannarala commented 4 years ago

Hi @zeshuai007 Thank you for the quick response. when i have some Japanese text for example "title": "テストサンプルテキスト" in the above sample when i transform the JSON i am getting some thing like this "title" : "?????????? ". Am i missing something to achieve that. Secondly what is the prominence of the below notations

@(4,tracking_tag),
items.[&1]

And can you help me with the documentation of above please

zeshuai007 commented 4 years ago

Many "???" are just encoding problems, because I use UTF-8, you can delete or overwrite it with your local Japanese text. I ’m using the ‘modify-overwrite-beta’ operation. It only handles URL splicing. @ (4, tracking_tag) is just to resolve your URL-level errors. And

  "title": "= concat (@ (1, title), '')",
   "subtitle": "= concat (@ (1, note))",
   "imageUrl": "= concat (@ (1, img))", 

these changes to the Json structure are placed in the 'shift' operation.