Unexpected token u with node-red - node.js

I am facing an error while parsing my string to json, I have a string that looks like this
{"rxpk":[{"tmst":13885860,"time":"2016-04-13T12:52:17.085935Z","chan":1,"rfch":1,"freq":868.300000,"stat":1,"modu":"LORA","datr":"SF12BW125","codr":"4/5","lsnr":6.0,"rssi":-45,"size":15,"data":"QAMBAQKAPgIFXA2ikKV7"}]}
I am using node-red to create a flow diagram to parse it, I have a function block which makes the following
obj = JSON.parse(msg.payload);
var data = obj.rxpk[0].data;
return data
However, I get the Unexpected token u. What I am doing wrong?
Thanks in advance,
Regards!
EDIT: adding pic, I have something as show in the picture, I have replaced my function that was doing the JSON.parse by the block json.format, thus unexpected token is gone, but still I cannot retrieve my data field from the json string

Related

ParseJson not valid When message is received from topic auto complete in logic app

I am new in the logic app. I want develop one new logic app when message received from topic content data couldn't parse as json , its throwing error like **"
InvalidJSON. The 'content' property of actions of type 'ParseJson' must be valid JSON. The provided value cannot be parsed: 'Unexpected character encountered while parsing value: e. Path '', line 0, position 0.'
."**
after decode my string my json like below
{"Id":"ddd9cc1e-8e41-4684-be2e-cd874398e209","Type":"userData","Content":[{"EmpId":1,"EmpName":"Raja1"},{"EmpId":2,"EmpName":"Raja2"}]}
Any one suggest whats wrong am doing here? Attached some screenshot for reference.
enter image description here
enter image description here
As you are trying to convert an xml to json. You are very close - only issue is #json() expects either
A string that is a valid JSON object.
An application/xml object to convert to JSON.
The #base64toString() is converting to a string, but you really need to let #json() know this is #2 and not #1, so changing expression to this should work:
#{json(xml(base64toBinary(triggerBody()[contentdata])))[foo]}

Key not being recognized in JSON

I have following JSON that i am trying to parse.
{"id":1,"colour":"blue","count(colour)":1}
It is a result of what i have returned from my sqlite3 select staement. I am doing a count(colours), which is being returned as the key in the JSON.
Then when i attempt to reference the value using .count(colour), my node app fails giving me an error that colour is not defined. Note, that referencing .id works just fine.
Has anybody ran into this issue before or can provide any help?
If your JSON is in some variable, myjson, and you access it directly with myjson.count(colours), you get the error because it's trying to execute a function in your object.
It works as you expect if you access via string like this: myjson["count(colour)"].

Should I Parse JSON Data Before Inserting To MongoDB?

So, I am receiving some JSON data from a client to my Node.JS server. I want to insert that json into my MongoDB instance using Mongoose.
I can insert the JSON as-is, and it works great, because it's just text. However, I want to parse it before insertion so that when I extract it later it will be all nice and neat.
So, this works:
wordStream.words.push(wordData);
And this doesn't:
wordStream.words.push(JSON.parse(wordData));
So, should I even want to parse the JSON before insertion?
And if I should parse the JSON, how do I do it without throwing an error? I need to put everything in double quotes "", I believe, before it will parse, but for some reason whenever I make a string with double quotes and parse it, it turns everything all wrong.
Here is the JSON:
{ word: 'bundle',
definitions:
[ { definition: 'A group of objects held together by wrapping or tying.',
partOfSpeech: 'noun' } ],
urlSource: 'testurl',
otherSource: '' }
And the error when I try to parse
/Users/spence450/Documents/development/wordly-dev/wordly-server/node_modules/mongoose/lib/utils.js:409
throw err;
^
SyntaxError: Unexpected token o
Ideas?
So, should I even want to parse the JSON before insertion?
Convert the strings to JSON objects will benefit you later, when you need to make queries in your MongoDB database.
And if I should parse the JSON, how do I do it without throwing an error? I need to put everything in double quotes "", I believe, before it will parse, but for some reason whenever I make a string with double quotes and parse it, it turns everything all wrong.
You aren't receiving JSON documents. JSON documents must contain the keys quoted.
You can:
Use a library that recognizes invalid JSON objects (please don't)
Use eval (this is a security issue, so don't do it)
Fix the source of the problem, creating real JSON objects. This isn't difficult, you can see the JSON features here

Using 'querystring.parse' built-in module's method in Node.JS to read/parse parameters

Scenario:
Consider the following code:
var querystring = require('querystring');
var ParamsWithValue = querystring.parse(req._url.query);
Then I am able to read any query string's value.
E.g: If requested string is http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324
I can get the values of query string with codes ParamsWithValue.UID & ParamsWithValue.FacebookID respectively.
Issue: I am able to get the values of any number of parameters passed in the same way described above. But for second time onwards I am getting the following error in response on browser.
Error:
{"code":"InternalError","message":"Cannot read property 'query' of undefined"}
Question: What is wrong in the approach to read the query string from the URL.
Note: I don't want to use any frameworks to parse it. I am trying to depend on built-in modules only.
Update: It responds correctly when the value of any of the parameter is changed. But if the same values requested again from even different browser it throws same error.
I think you need req.url rather than req._url.
req.url is a string, if you want a URI instance use require('url').parse(req.url)
So, you should finally have:
var ParamsWithValue = querystring.parse(require('url').parse(req.url).query);
Edit: I corrected a typo in point 1, the last req.url -> req._url

how to refresh yui3 datatable with updated json data

I want to refresh datatable using new data in json format.
I tried using below method but it is giving error mentioned
var myData = table.get('data');
myData.add(json_data);
Error in console log:
invalid 'in' operand config
userTargets = (config && BUBBLETARGETS in config);
Please can someone shed some light if there's any other menthod to refresh yui3 datatable using new data
Thanks in advance.
It sounds like the json data you're passing it isn't an array of objects. Maybe it's an object with the array of objects nested inside somewhere?
At any rate, you can do
table.set('data', json_data.path.to.resultArray);
If you want to append more records to the table, try
table.addRows(json_data.path.to.resultArray);
HTH
It worked fine when I used eval to json_data (which was encoded in Perl from string into Json format)
table.set('data', eval(json_data));

Resources