You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- text: "Thank you for your post! How to avoid creating duplicated keys in json?"
16
+
- text: "Payload:"
17
+
- code: |
18
+
{"status":true}
19
+
lang: json
20
+
- text: "Schema:"
21
+
- code: |
22
+
output application/json encoding="UTF-8"
23
+
---
24
+
++(payload, payload)
25
+
lang: dataweave
26
+
- text: "And the result is invalid json:"
27
+
- code: |
28
+
{
29
+
"status": true,
30
+
"status": true
31
+
}
32
+
lang: json
33
+
- author: "Alex Martinez"
34
+
date: 2022-11-03
35
+
replyTo: "Erki Kriks"
36
+
parts:
37
+
- text: "Well, in this case since you're concatenating the same object, it's expected that you'll have duplicated keys and values. But you can use the `distinctBy` function to get rid of duplicated keys. Like so:"
38
+
- code: |
39
+
output application/json encoding="UTF-8"
40
+
---
41
+
++(payload, payload)
42
+
distinctBy $
43
+
lang: dataweave
44
+
- author: "Erki Kriks"
45
+
date: 2022-11-03
46
+
replyTo: "Alex Martinez"
47
+
parts:
48
+
- text: "But it does not work if values are different."
49
+
- text: "Payload:"
50
+
- code: |
51
+
{
52
+
"status": null,
53
+
"message": "Hello world!"
54
+
}
55
+
lang: json
56
+
- text: "Schema:"
57
+
- code: |
58
+
%dw 2.0
59
+
output application/json
60
+
---
61
+
++ (payload, {"status":true}) distinctBy $
62
+
lang: dataweave
63
+
- text: "Result is invalid json:"
64
+
- code: |
65
+
{
66
+
"status": null,
67
+
"message": "Hello world!",
68
+
"status": true
69
+
}
70
+
lang: json
71
+
- author: "Alex Martinez"
72
+
date: 2022-11-03
73
+
replyTo: "Erki Kriks"
74
+
parts:
75
+
- text: "Whoops! My mistake. Instead of using `$` (which refers to the value), you should be using `$$` (which refers to the key)"
76
+
- code: |
77
+
%dw 2.0
78
+
output application/json
79
+
---
80
+
++ (payload, {"status":true}) distinctBy $$
81
+
lang: dataweave
82
+
- author: "Erki Kriks"
83
+
date: 2022-11-04
84
+
replyTo: "Alex Martinez"
85
+
parts:
86
+
- text: "Ok, thanks! This works. In JavaScript there is a similar function: `Object.assign(json1, json2)`; So, if same key is used in first json, it becomes overwritten by second json."
87
+
- text: "In dataweave, fields in second json will be ignored if they already exists in first json. Of course, we can change the order to ignore fields in first json if they exists in second json:"
88
+
- code: |
89
+
++ ({"status":true}, payload) distinctBy $$
90
+
lang: dataweave
11
91
---
12
92
13
93
In this post, I will explain what object concatenation is and how to utilize it.
0 commit comments