Forum Discussion

jfpelletier's avatar
jfpelletier
Contributor
2 years ago
Solved

How to get all values of a key from objects in a list?

Hello all,

I'm a bit blocked at the moment, hopefully someone can help me again... πŸ™‚

This time I have simple objects stored in an array that looks like this:

[
	{
		"language": "German",
		"id": "43251-1-2"
	},
	{
		"language": "French (Europe)",
		"id": "43251-1-3"
	},
	{
		"language": "Italian",
		"id": "43251-1-4"
	}
]

From that list, I need to extract the values of the "language" keys and put them in a list like this:

["German", "French (Europe)","Italian"]

I suspect that I need to use the .reduce function on the array first, then get the value of the "language" keys, but all my attempts failed... I must say that I'm not very familiar with the .reduce function, I'm just hoping that I was in the right direction... And it's probably much simpler than I think it is...

Thanks in advance for your help! πŸ™‚

JF

  • If your data is a field like languages, you could use $languages.map(obj => obj.language) to pull just those values out.

9 Replies

  • If your data is a field like languages, you could use $languages.map(obj => obj.language) to pull just those values out.

    • jfpelletier's avatar
      jfpelletier
      Contributor

      Hello ddellsperger,

      Yes! That works perfectly! I was looking at this wrong... πŸ˜‹

      Thanks a lot and have a great rest of the day.

      JF

  • you can also use jsonPath, which if you pull the "language" field of your field into the Expression of your Mapper snap, it should correctly create the syntax to return those values for you.

    • jfpelletier's avatar
      jfpelletier
      Contributor

      Hello tbranco,

      Thanks a lot, that's awesome! I'll share this with my team so that we can all learn.

      Just one thing however... The reduce() method seems to work, but it's also returning an error message:

      Is this just a typo, or we can safely ignore the message?

      Kind regards,

      JF

      • tbranco's avatar
        tbranco
        Employee

        Hi JF,

        Yes you can safely ignore this message.

         

        Toni

  • Thanks also, koryknick

    I tried using jsonPath, but I was getting nowhere... The solution using map is very simple and clear, I'll continue using it, but thanks a lot for your suggestion too! πŸ™‚

    Kind regards,

    JF

  • Here is a short video showing how I proposed using JSONPath.