Stock Price Json Issue

I’ve done this many other times, but this json response looks a bit different which is perhaps what’s throwing me off, not sure.

I’m trying to simply map through the response to grab particular fields. I wrote the json response to a variable and then dropped it into json path finder (great tool!) to find the path. What is different about this path is that the numbered list doesn’t appear as the high level name. I had thought that mapping through wouldn’t be an issue – but there’s something I’m missing. What I’m trying to return is located at:

x.AMZN.symbol
x.IBM.symbol

When I used the map function to return values, I get an expression error. Any thoughts?

The code I’m using is below as is the json returned:

response = $context.response.data

myString = map(response,
              concat(x.symbol, "\r\n"
                    )
              )
join(myString, "\r\n")

JSON response:

{
	"AMZN": {
		"symbol": "AMZN",
		"name": "Amazon.com Inc",
		"exchange": "NASDAQ",
		"mic_code": "XNGS",
		"currency": "USD",
		"datetime": "2023-05-10",
		"timestamp": 1683742124,
		"open": "108.15000",
		"high": "110.20000",
		"low": "108.05000",
		"close": "109.53000",
		"volume": "52219578",
		"previous_close": "106.62000",
		"change": "2.91000",
		"percent_change": "2.72932",
		"average_volume": "74170408",
		"is_market_open": true,
		"fifty_two_week": {
			"low": "81.43000",
			"high": "146.57001",
			"low_change": "28.10000",
			"high_change": "-37.04001",
			"low_change_percent": "34.50816",
			"high_change_percent": "-25.27121",
			"range": "81.430000 - 146.570007"
		}
	},
	"IBM": {
		"symbol": "IBM",
		"name": "International Business Machines Corp",
		"exchange": "NYSE",
		"mic_code": "XNYS",
		"currency": "USD",
		"datetime": "2023-05-10",
		"timestamp": 1683742089,
		"open": "121.89000",
		"high": "122.43500",
		"low": "121.10000",
		"close": "121.53000",
		"volume": "1915690",
		"previous_close": "121.17000",
		"change": "0.36000",
		"percent_change": "0.29710",
		"average_volume": "3954609",
		"is_market_open": true,
		"fifty_two_week": {
			"low": "115.54500",
			"high": "153.21001",
			"low_change": "5.98500",
			"high_change": "-31.68001",
			"low_change_percent": "5.17980",
			"high_change_percent": "-20.67751",
			"range": "115.544998 - 153.210007"
		}
	}
}

The map() functionality only works on array values ['list', 'of', 'things'] whereas the sample data you shared is an object {"key": "value"}.

Is it a fixed set of stocks you are retrieving? If so, you could access the properties directly.

I knew something was wrong with it.

No, not a static list of stocks. I’ll look for another source.

Thanks!

I just put together two new functions that should help with this use-case.

  • objectKeys(object) - outputs an array which contains the key names of the top-level properties of the input object
  • objectValues(object) - outputs an array which contains the values of the top-level properties of the input object
obj = { "first": "value1", "second": 2, "third": {"foo": "bar"}}
objectKeys(obj)   #\\ Output: ["first", "second", "third"]
objectValues(obj) #\\ Output: ["value1", 2, {"foo": "bar"}]

If you need the key and the value, you can get the object keys and use map to iterate through them while referencing the original object

obj = {
  "first":  { "name": "John" }
  "second": { "name": "Mark" }
  "third":  { "name": "Olivia" }
}
map(objectKeys(obj), concat("The name of the ", x , " person is ", obj[x].name))

Output:

["The name of the first person is John","The name of the second person is Mark","The name of the third person is Olivia"]

In your case, if you only wanted the ‘symbol’ property within the nested object, you could use objectValues to iterate directly through the values, ignoring the top-level keys.

response = $context.response.data

stockSymbols = map(objectValues(response), x.symbol)

join(stockSymbols, "\r\n")

This is incredible!

I’ll play around with it after awhile.

You are correct, I’m planning on pulling in other attributes other than just the symbol.

This works just as you outlined and completely solved what I was wanting to accomplish – thank you!

1 Like