Forum Discussion

vaidyarm's avatar
vaidyarm
Contributor
4 years ago

How to Convert Object with Key Values Pairs to Array of key values Pairs

This issue I face a lot of times and there is no simple solution I have found so far, let me know if anyone is able to do it 🙂

Input:

{
   "quotes":{
      "USDAED":3.673325,
      "USDAFN":63.849999,
      "USDALL":124.388,
      "USDAMD":476.287499,
      "USDANG":1.788825,
      "USDAOA":135.371665
   }
}

Output :

{
   "quotes":[
      {"USDAED":3.673325},
      {"USDAFN":63.849999},
      {"USDALL":124.388},
      {"USDAMD":476.287499},
      {"USDANG":1.788825},
      {"USDAOA":135.371665}
   ]
}

2 Replies

  • Hi @vaidyarm ,

    You can do this with a simple expression in a mapper.

    $quotes.entries().map(val => {...[val]})
    

    Target path: $quotes

    Input:

        {
            "quotes": {
                "USDAED": 3.673325,
                "USDAFN": 63.849999,
                "USDALL": 124.388,
                "USDAMD": 476.287499,
                "USDANG": 1.788825,
                "USDAOA": 135.371665
            }
        }
    

    Result:

      {
        "quotes": [
          {
            "USDAED": 3.673325
          },
          {
            "USDAFN": 63.849999
          },
          {
            "USDALL": 124.388
          },
          {
            "USDAMD": 476.287499
          },
          {
            "USDANG": 1.788825
          },
          {
            "USDAOA": 135.371665
          }
        ]
      }
    

    • vaidyarm's avatar
      vaidyarm
      Contributor

      Superb !!! worked exactly how I needed it, Thanks a lot !! 🙂