I have the below result, I want its show normal words like description: hello test, address:London in nodejs
delta result {"description":"HELLO Description Edit","address":{"street_address":"Business Edit","city":"Bradford EDIT"}}
I want like description: hello test, address:London
var pusthistory = new JobHistory({ changetype: 'Updated', details: JSON.stringify(delta) });
If your question is to convert a JSON to a normal string,
use
JSON.stringify() method to do that
Or if you want to parse JSON string to JSON,
You can parse it using JSON.parse()
Related
Is there a way to parse a user-entered MongoDB document (a string) into a mongodb/Mongoose document which can be inserted to database?
Example string
'{ "name": "Dave", "location_id" : ObjectId("5d8664a9b1b0ae25d60e0e42") }'
Yes, this can be done by utilising JSON.parse(), unfortunately, if that data you're passing does have an ObjectId() as the example string in your question does this will fail as it's invalid JSON.
Assuming your example string is wrong and you're not trying to pass in an object id you can do it this way:
...
const client = await mongodb.connect("connection-string");
const doc = JSON.parse('{ "name": "Dave", "location_id" : "5d8664a9b1b0ae25d60e0e42" }');
await client.db("my-db").collection("my-data").insertOne(doc);
...
If you are passing in invalid JSON such as the ObjectId then you will need to perform steps to remove it and pass in it's value as a string, and then add the location_id property manually before inserting:
...
doc.location_id = new mongodb.ObjectID(doc.location_id);
...
I am trying to get some JSON data from the API provided by the vendor, but i am getting error i try google it, in many comments people say to use JSON.Stringify but in my case JSON.Stringify didnt help me it returns an empty array like {}
repose from the api are as follow
[{ SYMBOL: 'FOREX',
CODE: 'REG',
LST: '219.50',
LTP: '12:52:35'}]
but my desire response is
[{ "SYMBOL" : "FOREX",
"CODE": "REG",
"LTP": '219.50',
"LST": '12:52:35'}]
If the JSON result provided by the vendor's API is indeed this :
[{ SYMBOL: 'FOREX',
CODE: 'REG',
LST: '219.50',
LTP: '12:52:35'}]
I have to inform you that this is invalid JSON. In JSON, the properties should be strings between double-quotes, otherwise it cannot be parsed.
Your desired response is the correct form. There is likely an error in the way the vendor is forming the output.
tl;dr : Your vendor's API is giving you a JavaScript Object, not JSON.
Your response is on text, To convert text to JSON string,
let text = `[{ SYMBOL: 'FOREX',
CODE: 'REG',
LST: '219.50',
LTP: '12:52:35'}]`
let jsonStr = JSON.stringify(eval("(" + text + ")")); // Convert Object String to JSON
console.log(jsonStr);
Note : Make sure that your vendor is trusted source, Because eval opens up your code for injection attacks, If your are worry about this, Please contact your vendor to provide JSON response.
Reference : Convert object string to JSON
Ok so what i did is use STRINGFY as a middleware and that solve my problem thanks for your help guys really appreciatable.
So I am trying to parse some data using mongoose my schema looks like
data: [{
'order-id': String,
'sku': String,
'purchase-date': String,
'payments-date': String,
and I am trying to get the data using :
let parseddata = new ParsedDataModel(doc) ;
parseddata.data[0].order-id // failed id is not defined
While
parseddata.data[0].sku // is working
Is this a problem with the dash 6 ? How can I solve it ?
The problem is with your "dash".
Node js thinks it is a "minus" and your id is another variable.
Try using the following:
parseddata.data[0]["order-id"]
PS: I would recommend using ObjectId as the type for order-id instead of String. You can then populate the order document directly from this itself.
I'm trying to figure out if it's possible to simply open up a JSON data script with nodejs and edit it... as javascript ...
// my json data
{
people : [{
name : 'Joe',
hobby : 'hunting',
job : 'accountant'
},{
name : 'William',
hobby : 'chess',
job : 'manager'
}]
}
i just want to do something like e.g.
people[0].name = 'Joseph'
so i'm trying
fs.open('/path/to/file', 'r+', function(err, fd){
// not really sure what to do from here...
})
there are plenty of answers about how to read/write text files... i just thought there might be an easier way for the case of a file in JSON
Whoops this is a repeat question - please see an earlier response here
How to update a value in a json file and save it through node.js
Parse the JSON using JSON.parse(), modify the parsed object, turn it back into a new string using JSON.stringify(), then save the string to the file.
am getting json data as a response in the following format
{ '{"select":"samplec","value":"nation"}': '' }
How can i get a valid json data like
'{"select":"samplec", "value": "nation"}'
That is sort of an odd response to be getting, but in any case for this particular example you would do something like:
// Get the keys of your weird response object
var keys = Object.keys(response);
// The first key is a JSON string, so parse that
var obj = JSON.parse(keys[0]);
If the response had more than one key, you could loop through them all and create an array of objects. I would look into why the response was formatted the way it is, though, and see if you can't get the JSON strings delivered in some other way.