Sam Khan
Sam Khan

Reputation: 92

Convert json string into json object in mule

Here is the scenario, I am reading data off SQL Server and its read in a map structure. The issue is that one of the fields is a JSON string but the expected output is a JSON list/object. Here is the source data (read inside the ESB): Source data:

{
    price_distributor=44.000000000, 
    width=0E-12, 
    tax_status=taxable, 
    web_ref=OLG, 
    stock_qty=0E-9, 
    price_dealer=40.000000000, 
    sync_uid=11, 
    shipping_class_id=null, 
    shipping_required=true, 
    attributes=[{"parent_sku":"BBB_BBBBB","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Black","termid":219,"termslug":"black","termuid":854},{"parent_sku":"AAA_AAAAA","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Green","termid":233,"termslug":"green","termuid":853},{"parent_sku":"YYYY_YYYYY","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Blue","termid":232,"termslug":"blue","termuid":848},{"parent_sku":"XXXX_XXXX","variation":1,"attid":18,"attname":"Fabric Type","attslug":"pa_fabric-type","attuid":837,"option":"Red","termid":235,"termslug":"red","termuid":851}]
}

The field in question is attributes and its content IS NOT a json array rather its a strigified array.

Here is the output:

{
  "price_distributor": "44",
  "width": "0",
  "tax_status": "taxable",
  "web_ref": "OLG",
  "stock_qty": "0",
  "price_dealer": "40",
  "sync_uid": 11,
  "shipping_class_id": null,
  "shipping_required": true,
  "attributes": "[{\"parent_sku\":\"BBB_BBBBB\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Black\",\"termid\":219,\"termslug\":\"black\",\"termuid\":854},{\"parent_sku\":\"AAA_AAAAA\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Green\",\"termid\":233,\"termslug\":\"green\",\"termuid\":853},{\"parent_sku\":\"YYYY_YYYYY\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Blue\",\"termid\":232,\"termslug\"

:\"blue\",\"termuid\":848},{\"parent_sku\":\"XXXX_XXXX\",\"variation\":1,\"attid\":18,\"attname\":\"Fabric Type\",\"attslug\":\"pa_fabric-type\",\"attuid\":837,\"option\":\"Red\",\"termid\":235,\"termslug\":\"red\",\"termuid\":851}]"
}

See the attributes field whereas the intended transformation should be:

{
  "price_distributor": "44",
  "width": "0",
  "tax_status": "taxable",
  "web_ref": "OLG",
  "stock_qty": "0",
  "price_dealer": "40",
  "sync_uid": 11,
  "shipping_class_id": null,
  "shipping_required": true,
  "attributes": [
      {
          "parent_sku":"BBB_BBBBB",
          "variation":1,
          "attid":18,
          "attname":"Fabric Type",
          "attslug":"pa_fabric-type",
          "attuid":837,
          "option":"Black",
          "termid":219,
          "termslug":"black",
          "termuid":854
        },
        {
            "parent_sku":"AAA_AAAAA",
            "variation":1,
            "attid":18,
            "attname":"Fabric Type",
            "attslug":"pa_fabric-type",
            "attuid":837,
            "option":"Green",
            "termid":233,
            "termslug":"green",
            "termuid":853
        },
        {
            "parent_sku":"YYYY_YYYYY",
            "variation":1,
            "attid":18,
            "attname":"Fabric Type",
            "attslug":"pa_fabric-type",
            "attuid":837,
            "option":"Blue",
            "termid":232,
            "termslug":"blue",
            "termuid":848
        },
        {
            "parent_sku":"XXXX_XXXX",
            "variation":1,
            "attid":18,
            "attname":"Fabric Type",
            "attslug":"pa_fabric-type",
            "attuid":837,
            "option":"Red",
            "termid":235,
            "termslug":"red",
            "termuid":851
        }
    ]
}

The dataweave transformation I used for attributes is this:

attributes: payload.attributes

I know this isn't correct but is there an operator/function that can help transform stringified json into a json object/list, something like json.parse() perhaps?

Your help is greatly appreciated.

Thanks, -Sam

Upvotes: 2

Views: 9040

Answers (2)

Attila
Attila

Reputation: 3406

This problem could be solved by using the "read" function.

You could consider using something like the following DataWeave 1.0 snippet:

%dw 1.0
%var content = { 
    key1: "value1", 
    attributes: "{ \"stringKey\":\"stringValue\" }"
}
%output application/json
---
read(content.attributes, "application/json")

Output:

{
  "stringKey": "stringValue"
}

See also the read function in the official reference:

The read function returns the result of parsing the content parameter with the specified mimeType reader.

The first argument points the content that must be read, the second is the format in which to write it. A third optional argument lists reader configuration properties.

Upvotes: 3

Sam Khan
Sam Khan

Reputation: 92

I figured it out. Here is the dataweave snippet for reference:

attributes:  (payload.attributes replace /\[|\]|[\\]|\"/ with "" splitBy "}") 
    map using(attarr = $ replace /\{|,\{/ with "" splitBy ",")
        {
            parent_sku: (attarr[0] splitBy ":")[1],
            variation: (attarr[1] splitBy ":")[1],
            attid: (attarr[2] splitBy ":")[1],
            attname: (attarr[3] splitBy ":")[1],
            attslug: (attarr[4] splitBy ":")[1],
            attuid: (attarr[5] splitBy ":")[1],
            option: (attarr[6] splitBy ":")[1]//,
            termid: (attarr[7] splitBy ":")[1],
            termslug: (attarr[8] splitBy ":")[1],
            termuid: (attarr[9] splitBy ":")[1]
        },

Upvotes: 0

Related Questions