All Questions

Tagged with
Filter by
Sorted by
Tagged with
1035 votes
31 answers
1.1m views

How to parse JSON using Node.js? [closed]

How should I parse JSON using Node.js? Is there some module which will validate and parse JSON securely?
Tikhon Jelvis's user avatar
888 votes
32 answers
935k views

How can I print a circular structure in a JSON-like format?

I have a big object I want to convert to JSON and send. However it has circular structure, so if I try to use JSON.stringify() I'll get: TypeError: Converting circular structure to JSON or ...
Harry's user avatar
  • 54.1k
852 votes
14 answers
774k views

Using Node.JS, how do I read a JSON file into (server) memory?

Background I am doing some experimentation with Node.js and would like to read a JSON object, either from a text file or a .js file (which is better??) into memory so that I can access that object ...
Matt Cashatt's user avatar
  • 23.9k
605 votes
11 answers
1.1m views

Proper way to return JSON using node or Express

So, one can attempt to fetch the following JSON object: $ curl -i -X GET http://echo.jsontest.com/key/value/anotherKey/anotherValue HTTP/1.1 200 OK Access-Control-Allow-Origin: * Content-Type: ...
MightyMouse's user avatar
  • 13.4k
591 votes
15 answers
221k views

Is it not possible to stringify an Error using JSON.stringify?

Reproducing the problem I'm running into an issue when trying to pass error messages around using web sockets. I can replicate the issue I am facing using JSON.stringify to cater to a wider audience: ...
Jay's user avatar
  • 19.5k
588 votes
8 answers
445k views

How can I pretty-print JSON using node.js?

This seems like a solved problem but I am unable to find a solution for it. Basically, I read a JSON file, change a key, and write back the new JSON to the same file. All works, but I loose the JSON ...
Rajat's user avatar
  • 33.6k
452 votes
13 answers
359k views

Is there a way to automatically build the package.json file for Node.js projects

Is package.json supposed to be manually edited? Couldn't a program like npm just look through the files, see the "require" statements, and then use that to put the necessary entries in the package....
neuromancer's user avatar
  • 54.7k
425 votes
8 answers
679k views

How do I consume the JSON POST data in an Express application

I'm sending the following JSON string to my server. ( { id = 1; name = foo; }, { id = 2; name = bar; } ) On the server I have this. app.post('...
neuromancer's user avatar
  • 54.7k
360 votes
11 answers
414k views

is there a require for json in node.js

I would like to include a couple of JSON files in my JavaScript code that are in the same directory as my JavaScript source file. If I wanted to include another JavaScript file I could simply use ...
Corno's user avatar
  • 5,246
353 votes
40 answers
1.7m views

"SyntaxError: Unexpected token < in JSON at position 0"

In a React app component which handles Facebook-like content feeds, I am running into an error: Feed.js:94 undefined "parsererror" "SyntaxError: Unexpected token < in JSON at position 0 I ran ...
Cameron Sima's user avatar
  • 5,216
332 votes
9 answers
807k views

Write / add data in JSON file using Node.js

I am trying to write JSON file using node from loop data, e.g.: let jsonFile = require('jsonfile'); for (i = 0; i < 11; i++) { jsonFile.writeFile('loop.json', "id :" + i + " square :" + i * i)...
Isoftmaster's user avatar
  • 3,385
298 votes
11 answers
357k views

How do I add environment variables to launch.json in VSCode

Working with the new VSCode editor on a node.js project. I am attempting to configure my "Launch" profile for debugging by editing the launch.json file. I need to setup a connectionstring as an ...
Jason Kibble's user avatar
  • 3,017
225 votes
17 answers
280k views

node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

[add] So my next problem is that when i try adding a new dependence (npm install --save socket.io). The JSON file is also valid. I get this error: Failed to parse json npm ERR! Unexpected string npm ...
IE8IsBetterThenGoogleChrome's user avatar
201 votes
9 answers
184k views

How do you log content of a JSON object in Node.js?

Is it possible to print an objects contents e.g. methods and attributes in Node.js? At the moment I'm trying to print the session object and get the following: console.log("Session:" + session); >...
Jack's user avatar
  • 15.8k
186 votes
3 answers
282k views

Convert a JSON Object to Buffer and Buffer to JSON Object back

I have a JSON object and I'm converting it to a Buffer and doing some process here. Later I want to convert the same buffer data to convert to valid JSON object. I'm working on Node V6.9.1 Below is ...
Prasanth J's user avatar
  • 1,863
163 votes
2 answers
233k views

In Node.js, how do I turn a string to a json? [duplicate]

For example, a HTTP REST API just returned me a JSON, but of course it's a string right now. How can I turn it into a JSON?
TIMEX's user avatar
  • 266k
148 votes
5 answers
187k views

Writing JSON object to a JSON file with fs.writeFileSync

I am trying to write a JSON object to a JSON file. The code executes without errors, but instead of the content of the object been written, all that gets written into the JSON file is: [object Object]...
Romulus3799's user avatar
  • 1,847
148 votes
13 answers
174k views

Parse large JSON file in Nodejs

I have a file which stores many JavaScript objects in JSON form and I need to read the file, create each of the objects, and do something with them (insert them into a db in my case). The JavaScript ...
dgh's user avatar
  • 9,089
131 votes
14 answers
141k views

Return certain fields with .populate() from Mongoose

I'm getting returned a JSON value from MongoDB after I run my query. The problem is I do not want to return all the JSON associated with my return, I tried searching the docs and didn't find a proper ...
Sleep Deprived Bulbasaur's user avatar
130 votes
4 answers
70k views

Is Maven similar to npm?

As I have worked with npm which looks for dependencies in package.json file and download it for you. Similarly, I see a pom.xml file in Java project. Does maven looks in this file and download ...
Shubham Jain's user avatar
  • 1,914
128 votes
8 answers
237k views

How to update a value in a json file and save it through node.js

How do I update a value in a json file and save it through node.js? I have the file content: var file_content = fs.readFileSync(filename); var content = JSON.parse(file_content); var val1 = content....
Nava Polak Onik's user avatar
111 votes
5 answers
128k views

Node js Error: Protocol "https:" not supported. Expected "http:"

I am using IBM Bluemix to make a web service for a school project. My project needs to request a JSON from an API, so I can use the data it provides. I use the http get method for a data set, and I ...
MBBertolucci's user avatar
  • 1,281
101 votes
9 answers
270k views

pass JSON to HTTP POST Request

I'm trying to make a HTTP POST request to the google QPX Express API [1] using nodejs and request [2]. My code looks as follows: // create http request client to consume the QPX API var ...
Ronin's user avatar
  • 7,662
100 votes
18 answers
372k views

TypeError: Converting circular structure to JSON in nodejs

I am using request package for node.js Code : var formData = ({first_name:firstname,last_name:lastname,user_name:username, email:email,password:password}); request.post({url:'http://localhost:8081/...
Hitu Bansal's user avatar
  • 3,017
98 votes
10 answers
170k views

Convert Mongoose docs to json

I returned mongoose docs as json in this way: UserModel.find({}, function (err, users) { return res.end(JSON.stringify(users)); } However, user.__proto__ was also returned. How can I return ...
Trantor Liu's user avatar
  • 8,980
94 votes
6 answers
194k views

Calling a JSON API with Node.js

I am trying to get the facebook profile picture of the user logged into my application. Facebook's API states that http://graph.facebook.com/517267866/?fields=picture returns the correct URL as a JSON ...
Sven's user avatar
  • 6,308
94 votes
21 answers
201k views

How to convert CSV to JSON in Node.js

I am trying to convert csv file to json. I am using . Example CSV: a,b,c,d 1,2,3,4 5,6,7,8 ... Desired JSON: {"a": 1,"b": 2,"c": 3,"d": 4}, {"a": 5,"b": 6,"c": 7,"d": 8}, ... I tried node-csv ...
Jetson John's user avatar
  • 3,809
93 votes
18 answers
179k views

Combine or merge JSON on node.js without jQuery

I have multiple JSON like those var object1 = {name: "John"}; var object2 = {location: "San Jose"}; They are not nesting or anything like that. Just basically different fields. I need to combine ...
HP.'s user avatar
  • 19.6k
93 votes
14 answers
67k views

Listen to All Emitted Events in Node.js

In Node.js is there any way to listen to all events emitted by an EventEmitter object? e.g., can you do something like... event_emitter.on('',function(event[, arg1][, arg2]...) {} The idea is that ...
Chris W.'s user avatar
  • 38.4k
89 votes
1 answer
54k views

In node package.json, invoke script from another script with extra parameter, in this case add mocha watcher

in node's package.json I would like to reuse a command that I already have in a 'script'. Here is the practical example instead of (note the extra -w on the watch script): "scripts": { ...
Dinis Cruz's user avatar
  • 4,239
86 votes
4 answers
88k views

Write formatted JSON in Node.js

I'm using Node.js to POST JSON to PostBin but the data is being wrongly formated (as you can see here: http://www.postbin.org/1cpndqw). This is the code I'm using for tesT: var http = require('http')...
donald's user avatar
  • 23.7k
83 votes
3 answers
118k views

How to return a complex JSON response with Node.js?

Using nodejs and express, I'd like to return one or multiple objects (array) using JSON. In the code below I output one JSON object at a time. It works but this isn't exactly what I want. The response ...
Martin's user avatar
  • 40k
77 votes
7 answers
61k views

Edit package.json from command line

I'm trying to add or edit a variable in my package.json from a shell script. So if i have a package.json like this: { "name": "my-project", "description": "Project by @DerZyklop", "version": "...
DerZyklop's user avatar
  • 3,763
74 votes
6 answers
199k views

Base64 encode a javascript object

I have large Javascript objects which I would like to encode to base-64 for AWS Kinesis` It turns out that: let objStr = new Buffer(JSON.stringify(obj), 'ascii'); new Buffer(objStr, 'base64')....
johni's user avatar
  • 5,498
74 votes
3 answers
235k views

Get a JSON via HTTP request in NodeJS

Here is my model with a JSON response: exports.getUser = function(req, res, callback) { User.find(req.body, function (err, data) { if (err) { res.json(err.errors); } ...
Sasha Grey's user avatar
  • 1,016
69 votes
6 answers
141k views

How to reliably hash JavaScript objects?

Is there a reliable way to JSON.stringify a JavaScript object that guarantees that the ceated JSON string is the same across all browsers, Node.js and so on, given that the JavaScript object is the ...
nh2's user avatar
  • 25.1k
68 votes
4 answers
56k views

JSON.stringify throws RangeError: Invalid string length for huge objects

As the title implies I'm trying to stringify huge JavaScript Object with JSON.stringify in my Node.js app. The objects are - again - huge (tens of mega bytes), they don't contain any functions. I need ...
borisdiakur's user avatar
  • 11.2k
67 votes
6 answers
80k views

Typescript compiler error when importing json file

So the code is simple: calls.json {"SERVER":{ "requests":{ "one":"1" } } } file.ts import json = require('../static/calls.json'); console.log(json.SERVER); the generated javascript ...
Ken's user avatar
  • 2,998
66 votes
24 answers
223k views

Node events.js:167 throw er; // Unhandled 'error' event

I'm trying to use JSON Server in a React App. However, I keep getting the following error. events.js:167 throw er; // Unhandled 'error' event ^ Emitted 'error' event at: at GetAddrInfoReqWrap....
frente_fin's user avatar
64 votes
8 answers
166k views

Parse XLSX with Node and create json

Ok so I found this really well documented node_module called js-xlsx Question: How can I parse an xlsx to output json? Here is what the excel sheet looks like: In the end the json should look like ...
Armeen Moon's user avatar
  • 18.5k
64 votes
10 answers
135k views

npm notice created a lockfile as package-lock.json. You should commit this file

I have been trying to load the skeleton of express with npm install express. It outputs the following line: npm notice created a lockfile as package-lock.json. You should commit this file. What ...
Venkateshreddy Pala's user avatar
63 votes
6 answers
37k views

Node.js -Firebase Service Account Private Key won't parse

I use .env variables in my app.js file to access the keys. Everything was working fine until I downloaded a new Firebase Service Account Private Key. When I replaced the old value with the new value I ...
Lance Samaria's user avatar
63 votes
1 answer
104k views

NodeJS HttpGet to a URL with JSON response

I'm trying to make a server-side API call using a RESTful protocol with a JSON response. I've read up on both the API documentation and this SO post. The API that I'm trying to pull from tracks busses ...
CaliCoder's user avatar
  • 663
62 votes
9 answers
215k views

Looping through JSON with node.js

I have a JSON file which I need to iterate over, as shown below... { "device_id": "8020", "data": [{ "Timestamp": "04-29-11 05:22:39 pm", "Start_Value": 0.02, "...
crawf's user avatar
  • 9,548
61 votes
6 answers
158k views

How can I check if a JSON is empty in NodeJS?

I have a function that checks to see whether or not a request has any queries, and does different actions based off that. Currently, I have if(query) do this else something else. However, it seems ...
thisissami's user avatar
  • 15.9k
60 votes
1 answer
30k views

Append a new object to a JSON Array in DynamoDB using NodeJS

I am using a JSON structure to store details of a person. Basically name, phone, registeredTimestamp etc., will be other attributes, and Primary key will be the email address. markedLocations, ...
bozzmob's user avatar
  • 12.5k
52 votes
17 answers
106k views

"Couldn't read dependencies" error with npm

I wanted to start a Node app and created a package.json file with a tutorial. Here is the json file: { "name": "Dashboard", "version": "0.0.0", "description": "Client-A Dashboard", "...
dvdmn's user avatar
  • 6,538
51 votes
8 answers
102k views

How to force parse request body as plain text instead of json in Express?

I am using nodejs + Express (v3) like this: app.use(express.bodyParser()); app.route('/some/route', function(req, res) { var text = req.body; // I expect text to be a string but it is a JSON }); I ...
pathikrit's user avatar
  • 33.2k
48 votes
5 answers
80k views

Handling bad JSON.parse() in node safely

Using node/express - I want to get some JSON out of request headers, but I want to do it safely. If for some reason it's not valid JSON, it's fine, it can just return false or whatever and it will ...
tpie's user avatar
  • 6,141
46 votes
12 answers
64k views

FormData append nested object

Is it possible to append nested object to FormData? let formData = new FormData(); let data = { title: 'title', text: 'text', preview: {p_title:'p title', p_text: 'p text'} }; $.each(...
ShibinRagh's user avatar
  • 6,596

1
2 3 4 5
236