I am new to javascript. I am trying to replace a JSON elements with a set values passed from another function. When i parse the object I am getting an error message unexpected token at pos 0.
Thank you.
var defaultMessage = {
"MTI": "1101",
dataElements: {
"DE01": "101",
"DE02": "201",
"DE03": "301",
"DE04": "401",
"DE05": "501",
"DE06": "601",
"DE07": "701"
}
}
replaceDefaultElements: (testElements) => {
console.info(testElements)
console.info(defaultMessage);
var messageElements = JSON.parse(defaultMessage.dataElements);
Object.keys(testElements).forEach(function (key) {
messageElements[key] = testElements[key];
});
return messageElements;
}
//Calling from JEST test function:
test('Should be ', () => {
expect(functions.replaceDefaultElements('{"MTI":"1101",{"DE01":"XXX"}}')).toEqual({
"MTI": "1101",
dataElements: {
"DE01": "XXX",
"DE02": "201",
"DE03": "301",
"DE04": "401",
"DE05": "501",
"DE06": "601",
"DE07": "701"
}
})
When i parse the object I am getting an error message unexpected token at pos 0.
defaultMessage.dataElements is a JavaScript object. It isn't JSON.
Don't use JSON.parse there.
testElements, on the other hand, is JSON.
Replace JSON.parse(defaultMessage.dataElements) with defaultMessage.dataElements
Replace Object.keys(testElements) with Object.keys(JSON.parse(testElements))
Hope it will work
Related
I'm currently working on a NodeJS project, this takes data from a JSON and then use it to take weather stuff form an API, after that I want to save it to a DB, I already asked about it and that question helped me fixing some problems but now I have some others, I'm sending the data to a constant but the issue is that I don't know why am I getting an error in the JSON Parse, I want to use the lat and lon from the JSON (I have like a hundred data coords) and insert it into the const, any help would help, This is the error I'm getting
Successful connection
[]
undefined:1
^
SyntaxError: Unexpected token in JSON at position 0
at JSON.parse (<anonymous>)
here is my function that takes data from JSON and gets data from the API:
async function calcWeather() {
fs.readFile("./json/data.json","utf8", function (err, data) {
if(err) throw err;
data2 = JSON.parse(data);
console.log(typeof data2);
for (let item of data2) {
let base = `https://api.openweathermap.org/data/2.5/weather?lat=${item.latjson}&lon=${item.lonjson}&appid=${api_key}&units=metric&lang=sp`;
fetch(base)
.then((responses) => {
return responses.json();
})
.then((data) => {
var myObject = {
Id_Oficina: item.IdOficina,
Humedad: data.main.humidity,
Nubes: data.clouds.all,
Sensacion: data.main.feels_like,
Temperatura: data.main.temp,
Descripcion: data.weather.description,
};
// validation and saving data to array
if (myObject.Temperatura < 99) {
lstValid.push(myObject);
}
});
}
});
console.log(lstValid);
}
here is the JSON where I take the data:
[
{
"latjson": 1,
"lonjson": 1,
"IdOficina": "1"
},
{
"latjson": 2,
"lonjson": 2,
"IdOficina": "2"
}
]
I think the issue is in the parse, but I don't get what I am doing wrong
Since you are reading the file with fs.readFile, you are getting a string and not a JavaScript object. You would need to parse it entirely in order to be able to manipulate the content (you seem to be parsing the first character only):
const fs = require('fs')
let rawdata = fs.readFileSync('./data.json')
let data = JSON.parse(rawdata)
Personally, I think it's way easier to require it (no need to use fs):
const jsonData = require('./json/data.json')
async function calcWeather() {
for (let item of jsonData) {
// ...
}
}
Am trying to update the key, value pair of a JSON file in protractor nodejs.Am unable to pass the 'key' param to the left-hand side of the 'equal to' operator in order to update the value in JSON file. Please find the below code and help me in resolving this :
Calling Method :
this.test = async function () {
await writeToJSON(title, test-name-07282020-1234);
}
Method:
async function writeToJSON(key, value) {
const { readFile, writeFile } = require('fs').promises;
const jsonFilePath = `${__dirname}/testdata.json`;
let data = JSON.parse(await readFile(jsonFilePath));
data.LANG.TEST2.Default.key = value; // line 5 ..due to key is not read from
// the parameter passed in the method. It is trying to look the field 'key' in
//json file to update the value.
writeFile(jsonFilePath, JSON.stringify(data, 2, 2)).then(() => {
console.log('Finished writing to json file')
})
}
testdata.json:
{
"LANG": {
"TEST1": {
"Default": {
"username": "value1",
"password": "value2"
}
},
"TEST2": {
"Default": {
"username": "value1",
"password": "value2",
"title": "test-name-05252020-4786"
}
}
}
It's failing at line 5
I want the key to read as 'title' and assign it the value 'test-name-07282020-1234'.Please help here!
you have to access with the brackets notation, it perfectly match for your use case:
data.LANG.TEST2.Default[key] = value;
I'm getting undefined trying to access some key inside of my JSON. The issue is because my value is inside of an Array.
I'm using console.log(jsonTest.data.eventHeader.id)
I've tried to use console.log(jsonTest.data[].eventHeader.id)
JSON Structure:
{ "data": [ { "eventHeader": { "id": "value" } } ] }
NodeJs code:
return rp(dataQuery) // simple query
.then((responseQuery: string) => {
JSON.parse(responseQuery);
const jsonTest: any = JSON.parse(responseQuery);
console.log(jsonTest.data.eventHeader.id)
fs.writeFileSync('liferaft-properties', jsonTest.username);
return responseQuery;
});
console.log(jsonTest.data[0].eventHeader.id) worked fine
I'm trying to create a process to parse a log file into a JSON file to a future processing.
The log file has this format:
[
{
"textPayload": "Dialogues0 gRPC Response : response_id: \"fc4e2e63-509b4ae2-a7d8-c401e563aa4b\"\nquery_result {\n query_text: \"wonderful\"\naction: \"smalltalk.appraisal.good\"\n parameters {\n }\nall_required_params_present: true\n fulfillment_text: \"Glad you think so!\"\n fulfillment_messages {\n text {\n text: \"Glad you think so!\"\n }\n }\n intent_detection_confidence: 1.0\n language_code: \"en\"\nsentiment_analysis_result {\n query_text_sentiment {\n }\n}\n}\nagent_id: \"3d22af45-f603-4a8a-a7ce-a9b2af47b762\"\n",
"insertId": "1lfux63g16s1nna",
"resource": {
"type": "global",
"labels": {
"project_id": "data-analytics-digital-dev"
}
},
"timestamp": "2018-11-07T14:31:02.435Z",
"severity": "INFO",
"labels": {
"request_id": "fc4e2e63-509b-4ae2-a7d8-c401e563aa4b",
"type": "dialogflow_response",
"protocol": "V2BETA1"
},
"logName": "projects/data-analytics-digital-dev/logs/dialogflow_agent",
"trace": "7fa08c8c-df50-4d46-9f20-b1e357b844a4",
"receiveTimestamp": "2018-11-07T14:31:02.555590714Z"
}
]
My target is the content of "textPayload"
the Node.js code is:
fs = require('fs');
fs.readFile('./global_logs1.json', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
let parsedLog = JSON.parse(data);
for(let myKey in parsedLog) {
let tempJson = (parsedLog[myKey]["textPayload"]);
tempJson = (tempJson.substr(91,));
tempJson = (tempJson.substr(0, tempJson.length - 50));
console.log((tempJson));
//console.log(JSON.parse(tempJson));
}
});
and it is resulting in a JSON like string:
{
query_text: "wonderful"
action: "smalltalk.appraisal.good"
parameters {
}
all_required_params_present: true
fulfillment_text: "Glad you think so!"
fulfillment_messages {
text {
text: "Glad you think so!"
}
}
intent_detection_confidence: 1.0
language_code: "en"
sentiment_analysis_result {
query_text_sentiment {
}
}
}
However when I call the JSON.parser I receive an error:
undefined:2
query_text: "wonderful"
^
SyntaxError: Unexpected token q in JSON at position 5
at JSON.parse (<anonymous>)
at c:\Codes\Logging\test.js:15:26
at FSReqWrap.readFileAfterClose [as oncomplete]
(internal/fs/read_file_context.js:53:3)
Seems it is missing double quotes, but I'm not sure.
Any idea on how to do it?
The text in the 'textPayload' attribute is absolutely not valid JSON. As you pointed out, the attribute names are missing double quotes. You are going to need to parse it out on your own. You could experiment with regex (results and opinions may vary.) or use existing "relaxed JSON" libraries that do not make use of eval.
EDIT: Using the module 'relaxed-json' I threw together this dirty script. You obviously can sanitize it yourself instead of relying on an external module but I am lazy here, and there might even be an out of the box solution to clean this up better, but this works. You need to remove the newlines, add commas after the items, add colons to the object attributes and add double quotes to the attribute names in order for it to be valid JSON. Like I said, its a dirty script and I did some awful regex matching and replacing just for the sake of making a proof of concept, so I am prepared to be judged on how crappy it is.
var parsed = require('./payload.json');
const rjson = require('relaxed-json');
for(let key in parsed){
let tempJson = (parsed[key]["textPayload"]);
tempJson = (tempJson.substr(91,));
tempJson = (tempJson.substr(0, tempJson.length - 50));
tempJson = tempJson.replace(/\n/g,",");
tempJson = tempJson.replace(/\{,/g,"{");
tempJson = tempJson.replace(/ \{/g,":{");
let transformed = JSON.parse(rjson.transform(tempJson));
console.log(transformed);
}
The output is a true javascript object now.
{ query_text: 'wonderful',
action: 'smalltalk.appraisal.good',
parameters: {},
all_required_params_present: true,
fulfillment_text: 'Glad you think so!',
fulfillment_messages: { text: { text: 'Glad you think so!' } },
intent_detection_confidence: 1,
language_code: 'en',
sentiment_analysis_result: { query_text_sentiment: {} } }
Your tempJson is actually a javascript object.
To convert it back to json just call
JSON.stringify(tempJson)
Sometimes the two concepts get mixed because they are so easily interchangeable.
I want to create the following JSON object, as seen from a console log:
Object
. member: Object
. id: 8286
I've been trying:
'member' :[{'id': 8286}]
but get the following error: "Uncaught SyntaxError: Unexpected token :"
What am I doing wrong? Thanks
var member = {
id: 8286
};
this allows you to access it like
member.id
You could have also meant something like:
var data = {
member: {
id: 8286
}
};
which you would access like so:
data.member.id
If this is not what you want, please clarify in your post cause I'm not sure I'm following your request correctly.
You're missing the curly braces surrounding the object.
As in:
var x = {'member': [{'id':8286}]};
You mean something like this?
{
"member": {},
"id": 8286
}
{
"member":{
"id":value
},
{
another_data:value
}
}
the data are accesses using the(.) operator.
the id is accessed by member.id