admin管理员组

文章数量:1122832

I have the input JSON

{
  "MainObj": {
    "SubObject": {
      "Invoices": [
        {
          "InvoiceItemsDetail": [
            {
              "InvoiceItemLineNumber": "1",
              "ItemCode": "122",
              "Description": "GoodsDescription1"
            },
            {
              "InvoiceItemLineNumber": "2",
              "ItemCode": "222",
              "Description": "GoodsDescription2"
            }
          ]
        },
        {
          "InvoiceItemsDetail": [
            {
              "InvoiceItemLineNumber": "3",
              "ItemCode": "322",
              "Description": "GoodsDescription3"
            },
            {
              "InvoiceItemLineNumber": "4",
              "ItemCode": "422",
              "Description": "GoodsDescription4"
            }
          ]
        }
      ]
    }
  }
}

I require the result which does not contain "AnotherObject" and "SomeOtherObject".

The resultant JSON will contain

  • InvoiceItemLineNumber converted to INVOICE_LINE_ITEM_NUMBER,

  • ItemCode to ITEM_CODE and

  • Description to DESCRIPTION

all of these inside an array of "outputs"

{
  "outputs": [
    {
      "INVOICE_LINE_ITEM_NUMBER": "1",
      "ITEM_CODE": "122",
      "DESCRIPTION": "GoodsDescription1"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "2",
      "ITEM_CODE": "222",
      "DESCRIPTION": "GoodsDescription2"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "3",
      "ITEM_CODE": "322",
      "DESCRIPTION": "GoodsDescription3"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "4",
      "ITEM_CODE": "422",
      "DESCRIPTION": "GoodsDescription4"
    }
  ]
}

I have the input JSON

{
  "MainObj": {
    "SubObject": {
      "Invoices": [
        {
          "InvoiceItemsDetail": [
            {
              "InvoiceItemLineNumber": "1",
              "ItemCode": "122",
              "Description": "GoodsDescription1"
            },
            {
              "InvoiceItemLineNumber": "2",
              "ItemCode": "222",
              "Description": "GoodsDescription2"
            }
          ]
        },
        {
          "InvoiceItemsDetail": [
            {
              "InvoiceItemLineNumber": "3",
              "ItemCode": "322",
              "Description": "GoodsDescription3"
            },
            {
              "InvoiceItemLineNumber": "4",
              "ItemCode": "422",
              "Description": "GoodsDescription4"
            }
          ]
        }
      ]
    }
  }
}

I require the result which does not contain "AnotherObject" and "SomeOtherObject".

The resultant JSON will contain

  • InvoiceItemLineNumber converted to INVOICE_LINE_ITEM_NUMBER,

  • ItemCode to ITEM_CODE and

  • Description to DESCRIPTION

all of these inside an array of "outputs"

{
  "outputs": [
    {
      "INVOICE_LINE_ITEM_NUMBER": "1",
      "ITEM_CODE": "122",
      "DESCRIPTION": "GoodsDescription1"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "2",
      "ITEM_CODE": "222",
      "DESCRIPTION": "GoodsDescription2"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "3",
      "ITEM_CODE": "322",
      "DESCRIPTION": "GoodsDescription3"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "4",
      "ITEM_CODE": "422",
      "DESCRIPTION": "GoodsDescription4"
    }
  ]
}
Share Improve this question edited 7 hours ago Barbaros Özhan 64.8k11 gold badges35 silver badges61 bronze badges asked yesterday user3691240user3691240 572 silver badges9 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

You just need "*": "[]" key-value pair as the innermost part of the transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "MainObj": {
        "SubObject": {
          "Invoices": {
            "*": {
              "InvoiceItemsDetail": {
                "*": "[]"
              }
            }
          }
        }
      }
    }
  }
]

the demo on the site Jolt Transform Demo Using v0.1.1 is :

EDIT : Rather you can use the following spec, considering that you need to rename the attributes for the new edited case :

[
  { //determine the objects seperated by upper indexes separately
    "operation": "shift",
    "spec": {
      "MainObj": {
        "SubObject": {
          "Invoices": {
            "*": {
              "InvoiceItemsDetail": {
                "*": {
                  "Invoice*": "&3_&1.INVOICE_LINE_ITEM_NUMBER",
                  "Item*": "&3_&1.ITEM_CODE",
                  "Desc*": "&3_&1.DESCRIPTION"
                }
              }
            }
          }
        }
      }
    }
  },
  { //get rid of the object keys while adding an array wrapper, namely "outputs"
    "operation": "shift",
    "spec": {
      "*": "outputs[]"
    }
  }
]

本文标签: Transform JSON using jolt transformationStack Overflow