Search code examples
jsonjolt

Store numbers keys as values with new key name in JOLT (part 2)


Based on my previous question I tried to modify a little bit different JSON, but I didn't succeed.

Source JSON:

{
  "data": {
    "ad_space": {
      "1080": {
        "conversion_page": {
          "58": {
            "day": {
              "2024-09-07": {
                "metrics": {
                  "reach_conversion": "1.0000000",
                  "tracked_conversion": 1
                }
              }
            }
          },
          "59": {
            "day": {
              "2024-09-07": {
                "metrics": {
                  "reach_conversion": "1.0000000",
                  "tracked_conversion": 1
                }
              }
            }
          },
          "60": {
            "day": {
              "2024-09-07": {
                "metrics": {
                  "reach_conversion": "1.0000000",
                  "tracked_conversion": 1
                }
              }
            }
          }
        }
      },
      "1095": {
        "conversion_page": {
          "58": {
            "day": {
              "2024-09-04": {
                "metrics": {
                  "reach_conversion": "1.0000000",
                  "tracked_conversion": 1
                }
              },
              "2024-09-05": {
                "metrics": {
                  "reach_conversion": "2.0000000",
                  "tracked_conversion": 1
                }
              }
            }
          },
....//etc

It's kinda big, and I cannot post full json here. I can provide some link: https://codeshare.io/WLABvv

JOLT config:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "ad_space": {
          "*": {
            "conversion_page": {
              "*": {
                "day": {
                  "*": {
                    "metrics": {
                      "$5": "&4_&2.id",
                      "$3": "&4_&2.conversion_page_id",
                      "$1": "&4_&2.report_date",
                      "*": "&4_&2.&",
                      "@6,metadata.conversion_page.&3.label": "&4_&2.&5_name"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "id": "=toInteger"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]

Result:

result

First problem is - somehow some values stores as arrays. Second problem is - values from metadata.conversion_page not displayed.


Solution

  • In the current case you have two more wrapper nodes, and need to add ;

    • 2 to "@6,metadata...." -> "@8,metadata...."
    • a new prefix 6_ to "&4_&2..." -> "&6__&4_&2..." which represents new non-fixed nodes

    and

    • convert &5_name to campaign_name in the new case, since there's no "campaign" key currently

    So, you can retry with this one :

    [
      {
        "operation": "shift",
        "spec": {
          "data": {
            "ad_space": {
              "*": {
                "conversion_page": {
                  "*": {
                    "day": {
                      "*": {
                        "metrics": {
                          "$5": "&6_&4_&2.id",
                          "$3": "&6_&4_&2.conversion_page_id",
                          "$1": "&6_&4_&2.report_date",
                          "*": "&6_&4_&2.&",
                          "@8,metadata.conversion_page.&3.label": "&6_&4_&2.campaign_name"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "id": "=toInteger"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]