2

How can I convert TEXT file into JSON in Dataweave.

Input:

"[
  {
    "quantity": "1.0",
    "uom": "every",
    "amount": "5.0",
    "allocation": [
      {
        "ID": "22245",
        "price": "156",
        "desc": "Product A1"
      }
    ],
  },
  {
    "quantity": "2.0",
    "uom": "all",
    "amount": "65888",
    "allocation": [
      {
        "ID": "65665",
        "price": "7789",
        "desc": "Product A2"
      }
    ],
  }
]"

Expected output in JSON:

ex:
[
  {
    "quantity": "1.0",
    "uom": "every",
    "amount": "5.0",
    "allocation": [
      {
        "ID": "22245",
        "price": "156",
        "desc": "Product A1"
      }
    ],
  },
  {
    "quantity": "2.0",
    "uom": "all",
    "amount": "65888",
    "allocation": [
      {
        "ID": "65665",
        "price": "7789",
        "desc": "Product A2"
      }
    ],
  }
]

enter image description here

aled
  • 21,330
  • 3
  • 27
  • 34
Vikash
  • 21
  • 2
  • The JSON output seems to be missing the curly brackets because of the ex key should be an object? – aled Feb 14 '22 at 11:30

1 Answers1

3

Use the read() function to parse the input string as JSON.

{
  ex: read(payload,"application/JSON")
}
aled
  • 21,330
  • 3
  • 27
  • 34
  • I am getting input in a TEXT file that need to convert in JSON – Vikash Feb 14 '22 at 11:52
  • The conversion is working. If the input has quotes it is parsed as a single JSON string. The format of your input doesn't match the input presented in the question. Which one is the right input? – aled Feb 14 '22 at 13:09