botframeworkbot-framework-composer

How to iterate through object keys / obtain keys of object


I am trying to create a dialog that displays a object in a dynamic manner in an adaptive card. Dynamic as in I don't know what the object keys are, how many keys there are, etc. Assuming that the object will not have nested arrays or other objects (basically the object will look like a map), how can I extract all the keys and put them in an array?

An example of an object that I wish to extract the keys from :

{
    "symbol": "MSFT",
    "companyName": "Microsoft Corporation",
    "primaryExchange": "Nasdaq Global Select",
    "sector": "Technology",
    "calculationPrice": "close",
    "open": 127.42,
    "openTime": 1556890200,
    "close": 128.9,
    "closeTime": 1556913600,
    "high": 129.43,
    "low": 127.25,
    "latestPrice": 128.9,
    "latestSource": "Close",
    "latestTime": "May 3, 2019",
    "latestUpdate": 1556913600,
    "latestVolume": 24835154,
    "iexRealtimePrice": null,
    "iexRealtimeSize": null,
    "iexLastUpdated": null,
    "delayedPrice": 128.9,
    "delayedPriceTime": 1556913600,
    "extendedPrice": 129.04,
    "extendedChange": 0.14,
    "extendedChangePercent": 0.00109,
    "extendedPriceTime": 1556917190,
    "previousClose": 126.21,
    "change": 2.69,
    "changePercent": 0.02131,
    "iexMarketPercent": null,
    "iexVolume": null,
    "avgTotalVolume": 22183270,
    "iexBidPrice": null,
    "iexBidSize": null,
    "iexAskPrice": null,
    "iexAskSize": null,
    "marketCap": 987737229888,
    "peRatio": 30.84,
    "week52High": 131.37,
    "week52Low": 93.96,
    "ytdChange": 0.30147812013916003
}

Solution

  • Use the select prebuilt function from Adaptive Expressions.

    It would be something like: select(myobject, x, x.key) and assigning that to a new property (or however you need to use that array).

    You can test this out further by using the expressions playground: https://playgroundclient.azurewebsites.net/

    I ran a quick test with the expression:

    select(myobject, x, x.key)
    

    And my data/properties as:

    {
      "myobject": {
        "symbol": "MSFT",
        "companyName": "Microsoft Corporation",
        "primaryExchange": "Nasdaq Global Select",
        "sector": "Technology",
        "calculationPrice": "close"
      }
    }
    

    And I get the result:

    ["symbol","companyName","primaryExchange","sector","calculationPrice"]
    

    Please let me know if I misunderstood your requirement/question. And if this answers your question; please mark as answered.