1

I have a string im working with from the JSON data below:

https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEtpWF9hQUhCWURZNEViUmpUeVgwdGc/1/public/basic?alt=json

You will notice the name of the companies is stored in a string:

ie:

"content": {
"type": "text",
"$t": "name: ANGLO AMERICAN, price: 1547, change: 8.5"

My problem is i want to pull out just the names of the companies. Currently i only have the string:"name: ANGLO AMERICAN, price: 1547, change: 8.5"

getting there is in the code below :

$(function() {
$.getJSON("https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEtpWF9hQUhCWURZNEViUmpUeVgwdGc/1/public/basic?alt=json", function(data) {
    console.log(data.feed.entry[0].content.$t);
    var foo = JSON.parse(data.feed.entry[0].content.$t);
    console.log(foo);

});
});

however i was hoping to turn the string into a set of objects like:

"name": "ANGLO AMERICAN",
"price": 1547,
"change": 8.5

Any advice on how to do this? or at least how to seperate the name from the string (keep in mind this is one in an array so the solution needs to work on all names in the data).

Thanks, Ewan

React Dev
  • 420
  • 2
  • 6
  • 16

3 Answers3

1

A valid JSON string is easily turned into objects, but the value you've got for "$t" is not valid JSON and no compliant parser will touch it (as you discovered). Your best option, if possible (it often isn't), would be to fix the code that's emitting that JSON-parody string. But clearly that's not a possibility here.

In cases where you can trust your source to keep cranking out this stuff in the same format for at least the near future, it's safe to fall back on an ugly regex solution -- just make sure it can handle every case the source may produce:

function parseCompany(str) {
    var re = /name:\s*([^,]+),\s*price:\s*(-?[\d\.]+|#N\/A),\s*change:\s*(-?[\d\.]+|#N\/A)/;

    var result = null;

    str.replace(re, function ($0, name, price, change) {
        result = { name: name, 
                   price: parseFloat(price), 
                   change: parseFloat(change)
                 };
    });

    return result
}

var co = parseCompany("name: ANGLO AMERICAN, price: 1547, change: 8.5");
alert(co.name);

co = parseCompany("name: BRIT AMER TOBACCO, price: 3407, change: -8.5");
alert(co.name);

You should never parse anything with regexes if a standard parser exists for the format (JSON, XML, etc.), but in this case you're on your own.

If there's a possibility of the fields changing order, you might want to break that up into three regexes.

UPDATE: Added "#N/A" handling for price and change. If those values are "#N/A", they'll will come out as NaN, JavaScript's magic "not a number" value.

0

Are you looking for JSON.parse(...)?

Parse JSON in JavaScript?

JSON.parse("{name: ANGLO AMERICAN, price: 1547, change: 8.5}")

yields

{"name": "ANGLO AMERICAN",
"price": 1547,
"change": 8.5}
Community
  • 1
  • 1
Andreas
  • 4,937
  • 2
  • 25
  • 35
  • Hi Andreas,thanks for the reply. I am, i tried JSON.pase (you can see in the code above, should the code above work?) but i got this in my console? Uncaught SyntaxError: Unexpected token a script.js:51 – React Dev Apr 18 '14 at 18:45
  • @Andreas, when I tested that code in Chrome, it threw an exception on the first n in 'name', because it's not double quoted. "ANGLO AMERICAN", "price", and "change" would have to be double-quoted as well, for [a compliant JSON parser](http://stackoverflow.com/questions/2067974/in-json-why-is-each-name-quoted) to accept the string. – 15ee8f99-57ff-4f92-890c-b56153 Apr 18 '14 at 18:49
0

For anyone on the same quest as me, I have managed to get around this problem by downloading it as a CSV from this site and loading it into my database:

http://www.ftsefeed.co.uk/api.html

It's not live like the JSON but seems to avoid all the tedium involved in the web API.

If mongo DB is your database look here for more info:

how to import csv file in mongodb

ER

Community
  • 1
  • 1
React Dev
  • 420
  • 2
  • 6
  • 16