2

I am using an API to output a JSON file as such, and so cannot edit the format of the JSON file. Is there any way to be able to iterate through each intraday date, to find the value of open in each date?

I have tried using Object.intraday[0].open however this does not seem to work, as the it is not contained within an array? I realise that I can use Object.intraday.date.open (where date is e.g "2018-10-19 15:59:00:"); however I want to be able to index to the different times.

{
    "symbol": "AAPL",
    "stock_exchange_short": "NASDAQ",
    "timezone_name": "America/New_York",
    "intraday": {
        "2018-10-19 15:59:00:" {
            "open": "219.49",
            "close": "219.23",
            "high": "219.61",
            "low": "219.19",
            "volume": "302415"
        },
        "2018-10-19 15:58:00:" {
            "open": "219.62",
            "close": "219.48",
            "high": "219.70",
            "low": "219.48",
            "volume": "173762"
        },
             ....

This is the pascal code that I am using in order to do this, using a test JSON of {"intraday":{"2018-10-1915:59:00":{"open":"23","low":"4"},"2018-10-1915:58:00":{"open":"25","low":"21"}}}

  JSONValue := TJSONObject.ParseJSONValue('{"intraday":{"2018-10-1915:59:00":{"open":"23","low":"4"},"2018-10-1915:58:00":{"open":"25","low":"21"}}}');
  j:=0;
  begin
    if JSONVAlue is TJSONObject then
      begin
          // Get the quote and low values
          quote := JSONValue.GetValue<string>('intraday[0]');
          low := JSONValue.GetValue<string>('intraday.low');
          Memo1.Lines.Add(quote + ': ' + low);
          j := j+1;
      end;
   end;
5
  • Take a look at this Json Parser and this question. Also make intraday an array. Commented Jan 11, 2019 at 18:28
  • Also here's a example on how it should look like and to do it. Commented Jan 11, 2019 at 18:35
  • @AdriaanBoshoff I can't modify the JSON file, as it is taken from an API. Is there any way of maybe iterating through the different intraday times to find their corresponding "open" value for example? Commented Jan 11, 2019 at 18:46
  • intraday is a single object with nested objects that use dates as their field names. That is a weird design choice. It would have made more sense for intraday to be an array of objects that have their dates as string fields. Whoever designed this API doesn't understand JSON and OOP very well. Commented Jan 11, 2019 at 22:09
  • @RemyLebeau I agree, but unfortunately there was very limited choice for a free, live stock database Commented Jan 11, 2019 at 22:28

1 Answer 1

3
procedure TfmMain.ParseIntraDay(AMemo: TMemo);
var
  LObject, LIntraDayObj: TJSONObject;
  LEnumerator: TJsonPairEnumerator;
  LQuote, LLow: String;
begin
  LObject := TJSONObject.ParseJSONValue('{"intraday":{"2018-10-19 15:59:00":{"open":"23","low":"4"},"2018-10-19 15:58:00":{"open":"25","low":"21"}}}') as TJSONObject;
  try
    LIntraDayObj := LObject.GetValue('intraday') as TJSONObject; //{"2018-10-19 15:59:00":{"open":"23","low":"4"},"2018-10-19 15:58:00":{"open":"25","low":"21"}}
    LEnumerator := LIntraDayObj.GetEnumerator;
    while LEnumerator.MoveNext do
    begin
      LQuote := LEnumerator.Current.JsonString.Value;
      LLow := (LEnumerator.Current.JsonValue As TJsonObject).Values['low'].Value;
      AMemo.Lines.Add(String.Format('%s: %s', [LQuote, LLow]));
    end;
   finally
     LObject.Free;
   end;
end;

Should give the following output in the memo:

2018-10-19 15:59:00: 4
2018-10-19 15:58:00: 21

Youcan use this as a basis for what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is what I was looking for. Didn't know about the getEnumerator function, good to know. So is this function just used to index the intraday values?
@Test1234 it is one way to do it, but it is not the only way. You could use the TJSONObject.Pairs property instead. But, if you are going to use the enumerator, you should use the friendlier for...in loop syntax instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.