Chizi
Chizi

Reputation: 51

How to add new fields to existing json

I have output json like this

[

"city": [
    {
      "street": "street"
      "zip": "223344"
    }
  ]
    "document": [
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "12345678",
      "file_name": "test.xml"
    }
  ]
]

and I want to add to "document" new fields so new output will be like this

[

"city": [
    {
      "street": "street"
      "zip": "223344"
    }
  ]
    "document": [
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "12345678",
      "file_name": "test.xml"
    },
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "123456789",
      "file_name": "test2.xml"
    },
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "1234567800",
      "file_name": "test3.xml"
    }
  ]
]

how can I add new fields? I tried do that with a help of code behind but unfortunately I didn't get correct result

payload map(value) -> value ++
document: vars.documentsInfo map {
    date: $.createdAt,
    id: $.id,
    file_name: $.name
  }

Upvotes: 0

Views: 1144

Answers (3)

short stack stevens
short stack stevens

Reputation: 692

If you are using dw 2.0 and mule runtime 4.2.2 (or later) you can use the update function.

Input

[
  {
    "city": [
      {
        "street": "street",
        "zip": "223344"
      }
    ]
  },
  {
    "document": [
      {
        "date": "2020-01-10T04:04:01-08:00",
        "id": "12345678",
        "file_name": "test.xml"
      }
    ]
  }
]

Dataweave Script

%dw 2.0
import * from dw::util::Values
output application/json
var data = [
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "123456789",
      "file_name": "test2.xml"
    },
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "1234567800",
      "file_name": "test3.xml"
    }
]
---
payload update "document" with flatten(payload.document + data)

Output

[
  {
    "city": [
      {
        "street": "street",
        "zip": "223344"
      }
    ]
  },
  {
    "document": [
      {
        "date": "2020-01-10T04:04:01-08:00",
        "id": "12345678",
        "file_name": "test.xml"
      },
      {
        "date": "2020-01-10T04:04:01-08:00",
        "id": "123456789",
        "file_name": "test2.xml"
      },
      {
        "date": "2020-01-10T04:04:01-08:00",
        "id": "1234567800",
        "file_name": "test3.xml"
      }
    ]
  }
]

Upvotes: 1

maddestroyer7
maddestroyer7

Reputation: 263

Unfortunately with dataweave, there's currently no good way to 'update' an array, field, or object. So the workaround is to save the initial value, combine it with the new objects that you want to add to the documents array, remove the initial information and add back the combined values. I did so here using do in dw2.0, hope it helps:

%dw 2.0
output application/json
var obj = {
  "city": [
    {
      "street": "street",
      "zip": "223344"
    }
  ],
  "document": [
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "12345678",
      "file_name": "test.xml"
    }
  ]
}

var documents = [
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "123456789",
      "file_name": "test2.xml"
    },
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "1234567800",
      "file_name": "test3.xml"
    }
]


---
do {
    var originalDoc = obj.document
    var newDocumentsArray = documents ++ originalDoc
    ---
    (obj - "document") ++ {documents: newDocumentsArray}
}

Upvotes: 0

utechtzs
utechtzs

Reputation: 1023

Input payload:

{
  "city": [
    {
      "street": "street",
      "zip": "223344"
    }
  ],
  "document": [
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "12345678",
      "file_name": "test.xml"
    }
  ]
}

A flowvar named "documentsInfo":

[
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "123456789",
      "file_name": "test2.xml"
    },
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "1234567800",
      "file_name": "test3.xml"
    }
]

The dataweave:

%dw 2.0
output application/json
---
{
    city: payload.city,
    documents: payload.document ++ vars.documentsInfo
}

Produces the output:

{
  "city": [
    {
      "street": "street",
      "zip": "223344"
    }
  ],
  "documents": [
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "12345678",
      "file_name": "test.xml"
    },
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "123456789",
      "file_name": "test2.xml"
    },
    {
      "date": "2020-01-10T04:04:01-08:00",
      "id": "1234567800",
      "file_name": "test3.xml"
    }
  ]
}

Upvotes: 0

Related Questions