0

I'm trying to write JavaScript to use parsed JSON, however the JSONP has a variable root, like this:

{"AAPL":{"quote":{"symbol":"AAPL","change":-3.34}},
 "FB":{"quote":{"symbol":"FB","change":1.03}},
 "TSLA":{"quote":{"symbol":"TSLA","change":2.9}}}


var stocks = ['AAPL', 'FB', 'TSLA'];
var stocksUrl = stocks.join(',');
var url = 'https://api.iextrading.com/1.0/stock/market/batch?symbols=' + stocksUrl + '&types=quote';
$.getJSON(url, function(data) {

After parsing with jquery, I can reference the JavaScript object such as: console.log(JSON.stringify(data.AAPL.quote.symbol)); }

However, since the length of the object is variable and the root element is variable, I am not sure how to use a for loop to iterate through the symbol and change elements.

My question is how to remove a variable root element and keep the sub-elements so that the object afterword would be referenced such that quote became the root element, ie:

for (var i = 0; i < Object.keys(data).length; i++) {
    console.log(JSON.stringify(data.quote.symbol));
}

I have tried Remove outer Object from certain key but keep inner objects but that solution is for a known element name. Can I do something similar with a variable object name? Or is there a better way to solve the problem?

Thanks in advance.

1
  • What do you mean by "variable root"? There doesn't seem to be a root item, there are just 3 properties in the object. Commented Oct 15, 2018 at 20:35

3 Answers 3

0

You can try something like this, in this case, you don't need to use the hardcore key name

var data = {"AAPL":{"quote":{"symbol":"AAPL","change":-3.34}},
             "FB":{"quote":{"symbol":"FB","change":1.03}},
         "TSLA":{"quote":{"symbol":"TSLA","change":2.9}}};

var keysArr= Object.keys(data);
for(var i=0;i<keysArr.length;i++){
    console.log(data[keysArr[i]].quote.symbol)
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works! Thank you.
0

Object.values(); is a method that will give you an array of just the values of an object.

So, this object

{
    "fb": {
        "a": 1,
        "b": 2,
        "c": 3
    },
    "google": {
        "a": 1,
        "b": 2,
        "c": 3
    },
    "ig": {
        "a": 1,
        "b": 2,
        "c": 3
    }
}

Could be turned into this:

var stuff_array = Object.values(stuff);
// turns into this
stuff_array = [
    {
        "a": 1,
        "b": 2,
        "c": 3
    },
    {
        "a": 1,
        "b": 2,
        "c": 3
    },
    {
        "a": 1,
        "b": 2,
        "c": 3
    }
]

So now you have an array, and you can loop over it with any one of about 10 different looping methods (.forEach, .map, .reduce, for loop, while loop).

1 Comment

I like this idea as well and will incorporate it into my script. Thank you.
0

You can modify the example you provided to work for you.

function removeJSONString(obj) {
  let keys = Object.keys(obj);

    keys.map(key => {
      Object.assign(obj[key], obj[key].quote);
      delete obj[key].quote;
    });

  return obj;
}

Here is a jsfiddle you can play around with to get what you intended.

2 Comments

Thanks Eric, I like the idea and tried to hack from the example with no luck. I'm not getting any console output from the fiddle.
@Bob I updated fiddle to show response in html.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.