How to Parse JSON with dynamic key

Dear Amibroker Team/Friends,
Please help me
How Parse JSON with dynamic key
it is important specially for nested json data or big data

Please find below code as example

Thanks
P.K. Behera

EnableScript("jscript");
<%
function _Parse( json_string )
{
	return JSON.parse( json_string );
}
%>

function ParseJSON( json_string )
{
   script = GetScriptObject();
   return script._Parse( json_string );
}

jsonData={
    "stockList": {
        "Stock0": "HDFCBANK,RELIANCE,LT,INFY,ADANIENT,SBIN,ADANIPORTS,ICICIBANK,TCS,BHARTIARTL,MARUTI,KOTAKBANK,ONGC,APOLLOHOSP,IDEA,ITC,BAJFINANCE",
        "Stock1": "TATASTEEL,AXISBANK,NTPC,TATAMOTORS,M&M,RECLTD,HINDUNILVR,HAL,BEL,PFC,COALINDIA,TITAN,DLF,HEROMOTOCO,BPCL,CANBK,HINDALCO",
        "Stock2": "VEDL,BHEL,WIPRO,TATAPOWER,INDUSINDBK,IRCTC,SUNPHARMA,HCLTECH,GAIL,IDFCFIRSTB,BANKBARODA,ASIANPAINT,HDFCLIFE,ASHOKLEY,AMBUJACEM,BATAINDIA,MOTHERSON",
        "Stock3": "TECHM,DIXON,CUMMINSIND,EXIDEIND,NMDC,BERGEPAINT,BAJAJFINSV,UPL,ULTRACEMCO,HINDPETRO,PNB,MUTHOOTFIN,VOLTAS,AUBANK,IGL,SAIL,GMRINFRA",
        "Stock4": "IPCALAB,INDIGO,TVSMOTOR,JINDALSTEL,TATACHEM,IOC,ABFRL,JUBLFOOD,POWERGRID,CIPLA,ACC,GRASIM,JSWSTEEL,DRREDDY,CHOLAFIN,INDUSTOWER,TATACONSUM",
        "Stock5": "TRENT,DIVISLAB,LTTS,NATIONALUM,AUROPHARMA,LICHSGFIN,BANDHANBNK,LTIM,BAJAJ-AUTO,SBILIFE,NESTLEIND,SBICARD,GUJGASLTD,PERSISTENT,FEDERALBNK,INDHOTEL,POLYCAB",
        "Stock6": "SHRIRAMFIN,HINDCOPPER,HDFCAMC,BIOCON,CONCOR,MGL,HAVELLS,PEL,IEX,PETRONET,COFORGE,DABUR,RBLBANK,ZEEL,MCX,APOLLOTYRE,BHARATFORG",
        "Stock7": "CUB,ALKEM,DEEPAKNTR,LUPIN,EICHERMOT,BSOFT,M&MFIN,OBEROIRLTY,MANAPPURAM,AARTIIND,CHAMBLFERT,BRITANNIA,MARICO,PVRINOX,PAGEIND,GLENMARK,LAURUSLABS",
        "Stock8": "ZYDUSLIFE,ASTRAL,ABCAPITAL,UBL,ABB,SIEMENS,GODREJPROP,TATACOMM,GNFC,BALKRISIND,BOSCHLTD,SRF,LTF,MPHASIS,PIDILITIND,NAUKRI,MCDOWELL-N",
        "Stock9": "IDFC,GRANULES,CANFINHOME,DALBHARAT,SUNTV,COROMANDEL,BALRAMCHIN,INDIACEM,GODREJCP,COLPAL,ICICIPRULI,ESCORTS,INDIAMART,PIIND,RAMCOCEM,NAVINFLUOR,CROMPTON",
        "Stock10": "MFSL,SYNGENE,ICICIGI,SHREECEM,OFSS,MRF,TORNTPHARM,METROPOLIS,ATUL,LALPATHLAB,JKCEMENT,ABBOTINDIA"
    }
}

ListNumber=Param("ListNumber",0,0,100,1);

list=ParseJSON( jsonData );
list=list.stockList;
Stocklist=list.Stock0;////Work fine

Stocklist=list.Stock+ListNumber; //Got Error but i want to use dynamic key

@pkb904 for the above task you do not need to parse any JSON data.

To get a "dynamic" variable you can use the VarGet() function

StockList0 = "HDFCBANK,RELIANCE,LT,INFY,ADANIENT,SBIN,ADANIPORTS,ICICIBANK,TCS,BHARTIARTL,MARUTI,KOTAKBANK,ONGC,APOLLOHOSP,IDEA,ITC,BAJFINANCE";
StockList1 = "TATASTEEL,AXISBANK,NTPC,TATAMOTORS,M&M,RECLTD,HINDUNILVR,HAL,BEL,PFC,COALINDIA,TITAN,DLF,HEROMOTOCO,BPCL,CANBK,HINDALCO";
StockList2 = "VEDL,BHEL,WIPRO,TATAPOWER,INDUSINDBK,IRCTC,SUNPHARMA,HCLTECH,GAIL,IDFCFIRSTB,BANKBARODA,ASIANPAINT,HDFCLIFE,ASHOKLEY,AMBUJACEM,BATAINDIA,MOTHERSON";
StockList3 = "TECHM,DIXON,CUMMINSIND,EXIDEIND,NMDC,BERGEPAINT,BAJAJFINSV,UPL,ULTRACEMCO,HINDPETRO,PNB,MUTHOOTFIN,VOLTAS,AUBANK,IGL,SAIL,GMRINFRA";
StockList4 = "IPCALAB,INDIGO,TVSMOTOR,JINDALSTEL,TATACHEM,IOC,ABFRL,JUBLFOOD,POWERGRID,CIPLA,ACC,GRASIM,JSWSTEEL,DRREDDY,CHOLAFIN,INDUSTOWER,TATACONSUM";
StockList5 = "TRENT,DIVISLAB,LTTS,NATIONALUM,AUROPHARMA,LICHSGFIN,BANDHANBNK,LTIM,BAJAJ-AUTO,SBILIFE,NESTLEIND,SBICARD,GUJGASLTD,PERSISTENT,FEDERALBNK,INDHOTEL,POLYCAB";
StockList6 = "SHRIRAMFIN,HINDCOPPER,HDFCAMC,BIOCON,CONCOR,MGL,HAVELLS,PEL,IEX,PETRONET,COFORGE,DABUR,RBLBANK,ZEEL,MCX,APOLLOTYRE,BHARATFORG";
StockList7 = "CUB,ALKEM,DEEPAKNTR,LUPIN,EICHERMOT,BSOFT,M&MFIN,OBEROIRLTY,MANAPPURAM,AARTIIND,CHAMBLFERT,BRITANNIA,MARICO,PVRINOX,PAGEIND,GLENMARK,LAURUSLABS";
StockList8 = "ZYDUSLIFE,ASTRAL,ABCAPITAL,UBL,ABB,SIEMENS,GODREJPROP,TATACOMM,GNFC,BALKRISIND,BOSCHLTD,SRF,LTF,MPHASIS,PIDILITIND,NAUKRI,MCDOWELL-N";
StockList9 = "IDFC,GRANULES,CANFINHOME,DALBHARAT,SUNTV,COROMANDEL,BALRAMCHIN,INDIACEM,GODREJCP,COLPAL,ICICIPRULI,ESCORTS,INDIAMART,PIIND,RAMCOCEM,NAVINFLUOR,CROMPTON";
StockList10 = "MFSL,SYNGENE,ICICIGI,SHREECEM,OFSS,MRF,TORNTPHARM,METROPOLIS,ATUL,LALPATHLAB,JKCEMENT,ABBOTINDIA";
// etc. 
ListNumber =  Param("ListNumber", 0, 0, 10, 1);

Stocklist = VarGet("StockList" + ListNumber); 
// display the value retrieved (the selected list....)
Title = Stocklist;

// sample usage of a comma-separated string list...
for( i = 0; ( symbol = StrExtract( Stocklist, i ) ) != ""; i++ )
{
  // body of the loop
  _TRACE("Symbol is " + symbol );
}

1 Like

Thank you for reply
My problem resolved by below code
I write a new code for this, which help to handle this problem

EnableScript("jscript");
<%
function _parseJSON(jsonString, key1, key2, key3, key4, key5 )
{
	var shell = new ActiveXObject("WScript.Shell");
    try
    {
        var jsonObject = JSON.parse(jsonString);      
        if((key1!=="")&&(key1 in jsonObject))
        {
			aaa = jsonObject[key1]		
			if((key2!=="")&&(key2 in aaa))
			{
				bbb = aaa[key2]	
				if((key3!=="")&&(key3 in bbb))
				{
					ccc = bbb[key3]			
					if((key4!=="")&&(key4 in ccc))
					{
						ddd = ccc[key4]
						if((key5!=="")&&(key5 in ddd))
						{
							eee = ddd[key5]
							return eee;					
						}
						else
						{
						return ddd;
						}						
					}
					else
					{
					return ccc;
					}							
				}
				else
				{
				return bbb;
				}		
			}
			else
			{
			return aaa;
			}   
        }
        else
        {
        //shell.Popup("SyntaxErr: Key1 is Empty or Mismatched");
        return jsonObject;
        }
        
    } catch (error)
    {
        shell.Popup("CatchErr: "+error.message);
        return Null;
    }
}
%>

function ParseJSON( json_string, Key1, Key2, Key3, Key4, Key5 )
{
	script = GetScriptObject();
	return script._parseJSON( json_string, Key1, Key2, Key3, Key4, Key5 );
}