0

The response for below Rest API call is

http://techpaisa.com/supres/m&m/

{
    "s": "M&M",
    "c": 1300.2,
    "e": false,
    "d": "20 March 2017",
    "l": [
        [1508.95, "52 Week High"],
        [1361.0, "Fibonacci Retracement Resisance"],
        [1360.0, "Call Option with Maximum Open Interest"],
        [1332.3500000000001, "R3 (Pivot Point)"],
        [1331.5685000000003, "200-Day Simple Moving Average"],
        [1326.8818166890858, "Average True Range (Upper Level)"],
        [1324.4000000000001, "R2 (Pivot Point)"],
        [1314.8699999999994, "20-Day Simple Moving Average"],
        [1312.3000000000002, "R1 (Pivot Point)"],
        [1307.73139784, "20-Day Exponential Moving Average"],
        [1304.3500000000001, "Pivot"],
        [1300.2, "Previous Closing Price (CP)"],
        [1300.0, "Put Option with Maximum Open Interest"],
        [1292.82354169, "200-Day Exponential Moving Average"],
        [1292.2500000000002, "S1 (Pivot Point)"],
        [1290.8456999999999, "Fibonacci Retracement (38% Level)"],
        [1284.7771330400001, "50-Day Exponential Moving Average"],
        [1284.3000000000002, "S2 (Pivot Point)"],
        [1273.5181833109143, "Average True Range (Lower Level)"],
        [1273.4080000000001, "50-Day Simple Moving Average"],
        [1272.2000000000003, "S3 (Pivot Point)"],
        [1269.175, "Fibonacci Retracement (50% Level)"],
        [1247.5043000000001, "Fibonacci Retracement (61% Level)"],
        [1177.3499999999999, "Fibonacci Retracement Support"],
        [1141.4000000000001, "52 Week Low"]
    ]
}

I am trying to extract Support values for a Stock

var symbol = jsonRes.s;   // Symbol  

Is it possible to extract these values (Support) from the above JSON ??

S1 (Pivot Point) , S2 (Pivot Point) , S3 (Pivot Point)

http://jsfiddle.net/cod7ceho/467/

Pawan
  • 31,545
  • 102
  • 256
  • 434
  • dont' know key name but the values will be always fixed like S3 (Pivot Point) , S2 (Pivot Point) , S1 (Pivot Point) – Pawan Mar 21 '17 at 10:43
  • how can i define that those 3 are support values? means you need to extract some set of values from the given json?? – Vara Prasad Mar 21 '17 at 10:43
  • Check out this http://stackoverflow.com/questions/9907419/javascript-object-get-key-by-value – VilleKoo Mar 21 '17 at 10:46

2 Answers2

2

statically we can find out.

 var ll = jsonRes.l;
 var s1 = ll[14];
 var s2 = ll[17];
 var s3 = ll[20];
console.log(s1[1])
console.log(s2[1])
console.log(s3[1])

webapi result is constant we can do as above.

Surya
  • 103
  • 1
  • 7
1

You can use startsWith() & endsWith() method like following.

var jsonRes = {
    "s": "M&M",
    "c": 1300.2,
    "e": false,
    "d": "20 March 2017",
    "l": [
        [1508.95, "52 Week High"],
        [1361.0, "Fibonacci Retracement Resisance"],
        [1360.0, "Call Option with Maximum Open Interest"],
        [1332.3500000000001, "R3 (Pivot Point)"],
        [1331.5685000000003, "200-Day Simple Moving Average"],
        [1326.8818166890858, "Average True Range (Upper Level)"],
        [1324.4000000000001, "R2 (Pivot Point)"],
        [1314.8699999999994, "20-Day Simple Moving Average"],
        [1312.3000000000002, "R1 (Pivot Point)"],
        [1307.73139784, "20-Day Exponential Moving Average"],
        [1304.3500000000001, "Pivot"],
        [1300.2, "Previous Closing Price (CP)"],
        [1300.0, "Put Option with Maximum Open Interest"],
        [1292.82354169, "200-Day Exponential Moving Average"],
        [1292.2500000000002, "S1 (Pivot Point)"],
        [1290.8456999999999, "Fibonacci Retracement (38% Level)"],
        [1284.7771330400001, "50-Day Exponential Moving Average"],
        [1284.3000000000002, "S2 (Pivot Point)"],
        [1273.5181833109143, "Average True Range (Lower Level)"],
        [1273.4080000000001, "50-Day Simple Moving Average"],
        [1272.2000000000003, "S3 (Pivot Point)"],
        [1269.175, "Fibonacci Retracement (50% Level)"],
        [1247.5043000000001, "Fibonacci Retracement (61% Level)"],
        [1177.3499999999999, "Fibonacci Retracement Support"],
        [1141.4000000000001, "52 Week Low"]
    ]
};

var support=[];
jsonRes.l.forEach(function(item){
    var text = item[1];
    if(text.startsWith('S') && text.endsWith('(Pivot Point)'))
        support.push({value: item[0], text: item[1]});
});
console.log(support);
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55