I have output from Twitch that goes like this:
#badges=moderator/1,subscriber/18,sub-gifter/5;room-id=[RoomID];emotes=;rm-received-ts=1675790149563;id=[LongString];subscriber=1;returning-chatter=0;client-nonce=[LongString];mod=1;turbo=0;badge-info=subscriber/23;user-type=mod;first-msg=0;historical=1;color=#00FFFF;display-name=usr;flags=;user-id=240978199;tmi-sent-ts=1675790149389 :usr!usr#usr.tmi.twitch.tv PRIVMSG #TwitchChannel :Test msg
I can parse it using str.split(";") but it's really badly formated and the params usually have a random order.
The best way how to do this for me, would be converting the whole string into JSON. Or should I convert it into array and then pick every item using str.startsWith('user-type') etc.? Is there any better way how you could do this?
Related
The data is stored as an array of objects wrapped in a string that looks like this
["{\"x\"=>15, \"y\"=>7}", "{\"x\"=>14, \"y\"=>7}", "{\"x\"=>13, \"y\"=>7}", "{\"x\"=>13, \"y\"=>6}", "{\"x\"=>13, \"y\"=>5}", "{\"x\"=>13, \"y\"=>4}", "{\"x\"=>13, \"y\"=>3}", "{\"x\"=>12, \"y\"=>3}", "{\"x\"=>11, \"y\"=>3}"]
The reason it is stored that way is because when I was storing the data from a json, I had to convert what was wrapped in Action Parameters to a hash.
I took a look at How to convert a ruby hash object to JSON? and Parse JSON in JavaScript?, and my answer is not addressed.
First, the problem is that it would seem JSON does not parse anything wrapped in double quotations, nor with rocket hash notation, and so I am not able to convert to convert "{"x"=>15, "y"=>7}" to {"x"=>15, "y"=>7}.
Perhaps, I have to serialize the object, see where I get my data from here: How can I access the data for the snake object sent through JSON in my params?
Any ideas on what the right approach would be?
Following radiantshaw's lead, using either
eval("{\"x\"=>15, \"y\"=>7}")
or
JSON.parse("{\"x\"=>15, \"y\"=>7}".gsub('=>', ':'))
I got the following: {"x"=>15, "y"=>7}, which is a Ruby object. However in order to convert this to a Javascript object, I also needed to convert it to json.
So by taking it one step further, I am able to parse it into json like so:
Put require 'json' in the .rb file, and then do {"x"=>15, "y"=>7}.to_json which will result in
"{\"x\":15,\"y\":7}".
The reason you're not able to convert to JSON because hash rocket is not a proper JSON syntax. Hash rocket is purely Ruby syntax.
What that means is that you somehow managed to take a Hash and convert it to a String. So the converted string is actually Ruby code and not JSON.
You could do...
eval("{\"x\"=>15, \"y\"=>7}")
... and it will return a Ruby Hash.
Or if you don't want to use eval due to security reasons, you can do...
JSON.parse("{\"x\"=>15, \"y\"=>7}".gsub('=>', ':'))
In netsuite i'm using the nlapiRequestURL to retrieve a JSON data from flexport, an overseas shipping company. I have have the data as a string(to my knowledge retrieving json data makes it a string) and want to turn it into an array of objects, but everything I have tried has resulted in various errors.
trying...
`var output = nlapiRequestURL(url,null,headers,"GET");
var split = JSON.parse(output.getBody());
response.write(split);`
gave me
{records=[Ljava.lang.Object;#7220fad}
and trying to show any element of split gave me undefined or that it cant read element from index.
I've ran the string through a JSON checker and it said it was a valid JSON file. I've done various variations of JSON.parse and looked tried Tostring. I've been working on this for a while and have no idea why I can't parse this information properly. Any help is appreciated.
You have parsed the result but then you are writing the parsed object which just gets you the object’s implementation dependent toString() output.
If you are just trying to echo the response re-stringify the parsed payload.
I'm planning to collect some data over a few months (daily) for further processing and representation in JavaScipt (probably using any js libraries, such as d3.js, etc. I don't know which one yet). The data will consist of:
date
one integer
one decimal number
Which file/data format would you recommend for recording data for subsequent work with JavaScript?
I think CSV would be more appropriate here because it sounds like it's just going to be a big long list of datapoints.
JSON would work, just like XML or any other system would work, but as much as I love JSON, it is not necessarily well-suited to the job:
JSON will be space-inefficient. It requires lots of punctuation characters.
JSON will be memory-inefficient. To add anything to the JSON file you'll need to:
Read the entire file as one long string
Parse that long string as JSON, saving it in memory as a big hash or array
Insert your new data
Re-encode the big hash or array as one long string
Overwrite your old file with that new long string
JSON will be harder to read... unless it's stored in "pretty" format, in which case it'll be even more space-inefficient.
CSV requires much less superfluous punctuation, and you can append a new line of data to the end of your file without needing to read and write the entire thing.
Consider:
JSON (not pretty):
{"01012016":[42,0.8675309],"01022016":[12,9.87654321]}
JSON (pretty):
{
"01012016":[
42,
0.8675309
],
"01022016":[
12,
9.87654321
]
}
CSV:
01012016,42,0.8675309
01022016,12,9.87654321
Javascript doesn't have a built-in CSV parser in the way it has JSON.parse... because parsing CSV is really easy! Here's just one of many ways of doing it:
var splitByLine = myCSVString.split("\n");
var splitByLineAndComma = [];
splitByLine.forEach(function(line){
splitByLineAndComma.push(line.split(","));
});
I would default to using JSON, which is not only lightweight, but JavaScript also has a built in, easy-to-use JSON parser: JSON.parse.
JSON is a collection of name/value pairs, which sounds ideal for the use case you've suggested:
[
{
"my-date": 1451744353495,
"my-int": 42,
"my-decimal": 3.1415926535
},
{
"my-date": 1451744353496,
"my-int": 43,
"my-decimal": 2.7182818284
},
{
"my-date": 1451744353497,
"my-int": 44,
"my-decimal": 1.4142135623
}
]
If you have JSON as a string, just pop it into JSON.parse, and you're ready to roll with a shiny new JavaScript object:
var obj = JSON.parse(str);
obj[0]["my-int"] === 42; // true
Given the url (string) http://stackoverflow.com/questions/ask, how could I split it up into an array that contains data like the following: questions,ask. How would I make this work for an infinite amount of slashes?
The way I am thinking of doing it would be to use URL.parse in Node.JS and then use STRING.split('/') to separate the string into an array.
Would this be the correct way to do it? Is there a quicker way?
To the best of my knowledge, that is the best way to do it. Alternatively you could just ignore the first parts of the split:
URL_STRING.split('/').slice(3)
I saw this string from an ajax call on some. It's clearly some sort of template. a:15 means there're 15 items in the {} expression. i:0 means item0, s:63: means the length of the string after it. I google for a while, but could not find any JS template engine that can take input like this one. It is possible they use Regex to parse the data.
a:15:{i:0;s:63:\"http://ww2.some.web/mw600/c01b8166jw1e4cf9fu2s0j20dw08v0v4.jpg\";i:1;s:63:\"http://ww4.some.web/mw600/c01b8166jw1e4cf9h284bj20dw0980ut.jpg\";i:2;s:63:\"http://ww1.some.web/mw600/c01b8166jw1e4cf9ksczrj20dw097n20.jpg\";i:3;s:63:\"http://ww3.some.web/mw600/c01b8166jw1e4cf9jvzymj20dw09840f.jpg\";i:4;s:63:\"http://ww2.some.web/mw600/c01b8166jw1e4cf9m9j9rj20dw0av41i.jpg\";i:5;s:63:\"http://ww3.some.web/mw600/c01b8166jw1e4cf9n1iq2j20dw0990ue.jpg\";i:6;s:63:\"http://ww2.some.web/mw600/c01b8166jw1e4cf9q062tj20dw09en17.jpg\";i:7;s:63:\"http://ww3.some.web/mw600/c01b8166jw1e4cf9sprglj20dw0a1djh.jpg\";i:8;s:63:\"http://ww1.some.web/mw600/c01b8166jw1e4cf9srts5j20dw097jui.jpg\";i:9;s:63:\"http://ww2.some.web/mw600/c01b8166jw1e4cf9wj84oj20dw08zn02.jpg\";i:10;s:63:\"http://ww1.some.web/mw600/c01b8166jw1e4cf9ws795j20dw09o418.jpg\";i:11;s:63:\"http://ww3.some.web/mw600/c01b8166jw1e4cf9xpixhj20dw0990ty.jpg\";i:12;s:63:\"http://ww3.some.web/mw600/c01b8166jw1e4cfa05o8fj20dw099die.jpg\";i:13;s:63:\"http://ww4.some.web/mw600/c01b8166jw1e4cfa0ah9yj20dw0aa76h.jpg\";i:14;s:63:\"http://ww3.some.web/mw600/c01b8166jw1ehttp://ww2.some.web/mw600/c01b8166jw1e4cf9fu2s0j20dw08v0v4.jpgcfa1jpsaj20dw099myq.jpg\";}
Looks like result of PHP serialize() function
You can use this js function to parse it.
This is PHP serialization format. You can unserialize with php:
unserialize(...)
And you'll get an array (with your example) if I'm not wrong