有没有办法根据键来映射它?

发布于 2025-01-09 07:05:45 字数 1454 浏览 0 评论 0原文

有没有办法根据键(“A”、“A-BOMB”等)映射下面的 JSON 示例?基本上我希望客户输入他们想要定义的单词,然后将其存储在变量中。假设他们要查找的单词存储在 vars.word 中,有没有一种方法可以映射 dataweave,以便它可以找到它并返回任何同义词、含义、同义词等?

{
"A": {
    "ANTONYMS": [],
    "MEANINGS": {
        "6": [
            "Noun",
            "the 1st letter of the Roman alphabet",
            [
                "Letter",
                "Letter of the alphabet",
                "Alphabetic character"
            ],
            []
        ],
        "7": [
            "Noun",
            "the blood group whose red cells carry the A antigen",
            [
                "Blood group",
                "Blood type"
            ],
            []
        ]
    },
    "SYNONYMS": [
        "Ampere",
        "Type a",
        "Antiophthalmic factor",
        "Angstrom",
        "Adenine"
    ]
},
"A-BOMB": {
    "ANTONYMS": [],
    "MEANINGS": {},
    "SYNONYMS": [
        "Atomic bomb",
        "Fission bomb",
        "Plutonium bomb",
        "Atom bomb",
        "A-bomb"
    ]
},
"A-HORIZON": {
    "ANTONYMS": [],
    "MEANINGS": {
        "1": [
            "Noun",
            "the top layer of a soil profile; usually contains humus",
            [],
            []
        ]
    },
    "SYNONYMS": [
        "A-horizon",
        "A horizon"
    ]
}

我已经

尝试过 filterObject ((value, key) -> valuestartsWith vars.word)

不确定是否应该使用过滤器、查找或startsWith函数。我不知道我在做什么。这甚至可能是不可能的。我只是不知道从哪里开始

Is there a way to map the below JSON example based on the keys ("A", "A-BOMB" etc)? Basically i want the client to input a word they want a definition for, and i store that in a variable. So lets say the word they're looking for is stored in vars.word, is there a way to map the dataweave so that it can find it and return the anyonyms, meaning, synonyms etc?

{
"A": {
    "ANTONYMS": [],
    "MEANINGS": {
        "6": [
            "Noun",
            "the 1st letter of the Roman alphabet",
            [
                "Letter",
                "Letter of the alphabet",
                "Alphabetic character"
            ],
            []
        ],
        "7": [
            "Noun",
            "the blood group whose red cells carry the A antigen",
            [
                "Blood group",
                "Blood type"
            ],
            []
        ]
    },
    "SYNONYMS": [
        "Ampere",
        "Type a",
        "Antiophthalmic factor",
        "Angstrom",
        "Adenine"
    ]
},
"A-BOMB": {
    "ANTONYMS": [],
    "MEANINGS": {},
    "SYNONYMS": [
        "Atomic bomb",
        "Fission bomb",
        "Plutonium bomb",
        "Atom bomb",
        "A-bomb"
    ]
},
"A-HORIZON": {
    "ANTONYMS": [],
    "MEANINGS": {
        "1": [
            "Noun",
            "the top layer of a soil profile; usually contains humus",
            [],
            []
        ]
    },
    "SYNONYMS": [
        "A-horizon",
        "A horizon"
    ]
}

}

I've tried filterObject ((value, key) -> value startsWith vars.word)

Not sure if i should use the filter, lookup or startsWith functions. I have no idea what I'm doing. It might not even be possible. I just don't even know where to start

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2025-01-16 07:05:45

注意-> startsWithcontains 不适合这里,因为您的对象具有以 A 开头的所有键,并且您的要求是匹配整个单词。

我认为使用仅数组/对象索引会对您有所帮助

DW

%dw 2.0
output application/json
var ip= "A-BOMB"
---
payload[ip]

使用 filterObject 以及 匹配 会给你相同的结果

DW

%dw 2.0
output application/json
var ip= "A-BOMB"
---
payload filterObject ((value, key) -> key matches ip)

输出

{
  "ANTONYMS": [
    
  ],
  "MEANINGS": {
    
  },
  "SYNONYMS": [
    "Atomic bomb",
    "Fission bomb",
    "Plutonium bomb",
    "Atom bomb",
    "A-bomb"
  ]
}

Note-> startsWith or contains wont fit here since your Object has all the keys starting with A and your requirement is to match the entire word.

I think using Just Array/Object indexing would help you here

DW

%dw 2.0
output application/json
var ip= "A-BOMB"
---
payload[ip]

Alternative approach using filterObject along with matches would give you same result

DW

%dw 2.0
output application/json
var ip= "A-BOMB"
---
payload filterObject ((value, key) -> key matches ip)

Output

{
  "ANTONYMS": [
    
  ],
  "MEANINGS": {
    
  },
  "SYNONYMS": [
    "Atomic bomb",
    "Fission bomb",
    "Plutonium bomb",
    "Atom bomb",
    "A-bomb"
  ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文