首页 > 解决方案 > Mulesoft 为数组中的嵌套属性显示空值(即使它在我引用外部属性时显示这些值。)

问题描述

我在读取嵌套属性值时遇到问题。我已将入站消息转换为以下结构 - 但是,当我将内部属性读取为 map 时,它会将它们显示为 null .. 但是如果我直接将父级拉到引用中,我可以看到内部值。入站有效载荷:

    [
  {
    "Name": "C-name1",
    "child": [
      {
        "ProductMessage__r": {
          "ProductMessageText__c": "testvalues",
          "Id": null,
          "type": "ProductMessage__c"
        },
        "Id": null,
        "type": "CallMessage__c",
        "Product__r": {
          "Id": null,
          "type": "Product__c",
          "Name": "stackov"
        }
      },
      {
        "ProductMessage__r": {
          "ProductMessageText__c": "stack overflow",
          "Id": null,
          "type": "ProductMessage__c"
        },
        "Id": null,
        "type": "CallMessage__c",
        "Product__r": {
          "Id": null,
          "type": "Product__c",
          "Name": "stackover"
        }
      }
    ]
  },
  {
    "callName": "C-02436592",
    "child": [
      {
        "ProductMessage__r": {
          "ProductMessageText__c": "stackover flow",
          "Id": null,
          "type": "ProductMessage__c"
        },
        "Id": null,
        "type": "CallMessage__c",
        "Product__r": {
          "Id": null,
          "type": "Product__c",
          "Name": "cooler"
        }
      },
      {
        "ProductMessage__r": {
          "ProductMessageText__c": "vxcvxcvxc",
          "Id": null,
          "type": "ProductMessage__c"
        },
        "Id": null,
        "type": "CallMessage__c",
        "Product__r": {
          "Id": null,
          "type": "Product__c",
          "Name": "Mixtard® 30 IND"
        }
      }
    ]
  }
]

现在在上面的有效负载中,如果我引用父值**(二、三)**,它会正确显示所有值,但如果我访问子参数一、四、五和六

%dw 2.0
output application/json
var t = payload.values
---
t map((item,index)->{
    callname:t[index].callName,
    childprod2: t[index].child map({
    one:    $. product__r.Name, //diplsys null
    two : $,//displays the entire nested values
    three: t[index].child,
    four : $. product__r, // displays null
    five: $. productmessage__r, //null
    six: $. productmessage__r. ProductMessageText__c //null

    })
    })

这是输出:它在引用外部属性时显示。但是当我引用内部属性时,它会将它们显示为空:

[
  {
    "callname": "C-02436590",
    "childprod2": [
      {
        "one": null,
        "two": {
          " ProductMessage__r": {
            " ProductMessageText__c": "stack overflowtestmix",
            "Id": null,
            "type": " ProductMessage__c"
          },
          "Id": null,
          "type": " CallMessage__c",
          " Product__r": {
            "Id": null,
            "type": " Product__c",
            "Name": " stack overflow 30 IND"
          }
        },
        "three": [
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflowtestmix",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          },
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflowtest",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          },
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflow stack overflowtest",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          }
        ],
        "four": null,
        "five": null,
        "six": null
      },
      {
        "one": null,
        "two": {
          " ProductMessage__r": {
            " ProductMessageText__c": "stack overflowtest",
            "Id": null,
            "type": " ProductMessage__c"
          },
          "Id": null,
          "type": " CallMessage__c",
          " Product__r": {
            "Id": null,
            "type": " Product__c",
            "Name": " stack overflow 30 IND"
          }
        },
        "three": [
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflowtestmix",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          },
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflowtest",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          },
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflow stack overflowtest",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          }
        ],
        "four": null,
        "five": null,
        "six": null
      }
    ]
  },
  {
    "callname": "C-02436592",
    "childprod2": [
      {
        "one": null,
        "two": {
          " ProductMessage__r": {
            " ProductMessageText__c": "stack overflowtestmix",
            "Id": null,
            "type": " ProductMessage__c"
          },
          "Id": null,
          "type": " CallMessage__c",
          " Product__r": {
            "Id": null,
            "type": " Product__c",
            "Name": " stack overflow 30 IND"
          }
        },
        "three": [
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflowtestmix",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          },
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflowtest",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          },
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflow stack overflowtest",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          }
        ],
        "four": null,
        "five": null,
        "six": null
      },
      {
        "one": null,
        "two": {
          " ProductMessage__r": {
            " ProductMessageText__c": "stack overflowtest",
            "Id": null,
            "type": " ProductMessage__c"
          },
          "Id": null,
          "type": " CallMessage__c",
          " Product__r": {
            "Id": null,
            "type": " Product__c",
            "Name": " stack overflow 30 IND"
          }
        },
        "three": [
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflowtestmix",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          },
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflowtest",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          },
          {
            " ProductMessage__r": {
              " ProductMessageText__c": "stack overflow stack overflowtest",
              "Id": null,
              "type": " ProductMessage__c"
            },
            "Id": null,
            "type": " CallMessage__c",
            " Product__r": {
              "Id": null,
              "type": " Product__c",
              "Name": " stack overflow 30 IND"
            }
          }
        ],
        "four": null,
        "five": null,
        "six": null
      }
    ]
  }
  

我浪费的最终结果是,对于每个电话,我都有一串“product-productmessage,product-productmessage”,但即使你只是帮助我访问和提取子值,它也会很棒。

标签: muledataweavemulesoft

解决方案


您可以使用此数据编织来提取子值。最好使用 item.callName 而不是 t[index].callName(虽然两者都是等价的)。map 函数本身迭代数组中的每个项目。


    %dw 2.0
    output json
    var t= payload
    ---
    t map(item,index) ->{
    callname:item.callName,
    childproduct2:item.child map {
    one: $.Product__r.Name,
    two:$,
    three:$,
    four: $.Product__r,
    five:$.ProductMessage__r,
    six:$.ProductMessage__r.ProductMessageText__c
    }
  }


推荐阅读