Node Js Php Serialize Format

Posted on  by
Node Js Php Serialize Format 5,0/5 906 reviews
Active5 months ago
  1. Node Js Php Serialize Format Of Resume
  2. Php Serialize Array
  3. Php Serialize Json
  4. Php 7 Vs Node Js

Home Products node-swiz – Node.js Library For Serializing, Deserializing And Validating Objects In REST. Products node-swiz – Node.js Library For Serializing, Deserializing And Validating Objects In REST APIs. Recently I started learning and working on Node.js platform. So, now I am again brushing up my long forgotten JavaScript skill. JSON is a common format you face every now and then in JavaScript, whether its client side or server side. I am switching my REST API from lumen to node, data was stored on redis in key value format but as lumen cache lib support serialize data so all the data was in serialize format in redis but In node.js to achieve that was need to use php-serializer package or use loop to decode data getting from redis.

How should I parse JSON using Node.js? Is there some module which will validate and parse JSON securely?

sampathsris
14.6k8 gold badges46 silver badges79 bronze badges
Tikhon JelvisTikhon Jelvis
56.4k16 gold badges156 silver badges198 bronze badges

31 Answers

12 next

You can simply use JSON.parse.

The definition of the JSON object is part of the ECMAScript 5 specification. node.js is built on Google Chrome's V8 engine, which adheres to ECMA standard. Therefore, node.js also has a global object JSON[docs].

Note - JSON.parse can tie up the current thread because it is a synchronous method. So if you are planning to parse big JSON objects use a streaming json parser.

Felix KlingFelix Kling
589k135 gold badges904 silver badges969 bronze badges

you can require .json files.

For example if you have a config.json file in the same directory as your source code file you would use:

or (file extension can be omitted):

note that require is synchronous and only reads the file once, following calls return the result from cache

Also note You should only use this for local files under your absolute control, as it potentially executes any code within the file.

grahamparks
14.9k5 gold badges41 silver badges41 bronze badges
eliocseliocs
13.4k6 gold badges35 silver badges47 bronze badges

You can use JSON.parse().

You should be able to use the JSON object on any ECMAScript 5 compatible JavaScript implementation. And V8, upon which Node.js is built is one of them.

Note: If you're using a JSON file to store sensitive information (e.g. passwords), that's the wrong way to do it. See how Heroku does it: https://devcenter.heroku.com/articles/config-vars#setting-up-config-vars-for-a-deployed-application. Find out how your platform does it, and use process.env to retrieve the config vars from within the code.

You'll have to do some file operations with fs module.

Asynchronous version

Synchronous version

You can sometimes use require:

But, I do not recommend this for several reasons:

  1. require is synchronous. If you have a very big JSON file, it will choke your event loop. You really need to use JSON.parse with fs.readFile.
  2. require will read the file only once. Subsequent calls to require for the same file will return a cached copy. Not a good idea if you want to read a .json file that is continuously updated. You could use a hack. But at this point, it's easier to simply use fs.
  3. If your file does not have a .json extension, require will not treat the contents of the file as JSON.

Seriously! Use JSON.parse.

If you are reading large number of .json files, (and if you are extremely lazy), it becomes annoying to write boilerplate code every time. You can save some characters by using the load-json-file module.

Asynchronous version

Synchronous version

If the JSON content is streamed over the network, you need to use a streaming JSON parser. Otherwise it will tie up your processor and choke your event loop until JSON content is fully streamed.

There are plenty of packages available in NPM for this. Choose what's best for you.

If you are unsure if whatever that is passed to JSON.parse() is valid JSON, make sure to enclose the call to JSON.parse() inside a try/catch block. A user provided JSON string could crash your application, and could even lead to security holes. Make sure error handling is done if you parse externally-provided JSON.

sampathsrissampathsris
14.6k8 gold badges46 silver badges79 bronze badges
Uli Köhler
9,1538 gold badges42 silver badges96 bronze badges
Mark KahnMark Kahn
65.5k23 gold badges144 silver badges196 bronze badges
pyprismpyprism
1,6015 gold badges34 silver badges55 bronze badges

I'd like to mention that there are alternatives to the global JSON object.JSON.parse and JSON.stringify are both synchronous, so if you want to deal with big objects you might want to check out some of the asynchronous JSON modules.

Have a look: https://github.com/joyent/node/wiki/Modules#wiki-parsers-json

sampathsris
14.6k8 gold badges46 silver badges79 bronze badges
HaiderHaider

Include the node-fs library.

For more info on 'fs' library , refer the documentation at http://nodejs.org/api/fs.html

Dan Dascalescu
76.6k23 gold badges217 silver badges297 bronze badges
Abhishek VermaAbhishek Verma

Since you don't know that your string is actually valid, I would put it first into a try catch. Also since try catch blocks are not optimized by node, i would put the entire thing into another function:

OR in 'async style'

VladVlad

Parsing a JSON stream? Use JSONStream.

Burcu DoganBurcu Dogan
8,0234 gold badges25 silver badges31 bronze badges

Everybody here has told about JSON.parse, so I thought of saying something else. There is a great module Connect with many middleware to make development of apps easier and better. One of the middleware is bodyParser. It parses JSON, html-forms and etc. There is also a specific middleware for JSON parsing only noop.

Take a look at the links above, it might be really helpful to you.

Saransh MohapatraSaransh Mohapatra
3,6269 gold badges28 silver badges47 bronze badges
Amal Murali
63.9k12 gold badges106 silver badges126 bronze badges
debianmasterdebianmaster

as other answers here have mentioned, you probably want to either require a local json file that you know is safe and present, like a configuration file:

or to use the global JSON object to parse a string value into an object:

note that when you require a file the content of that file is evaluated, which introduces a security risk in case it's not a json file but a js file.

here, i've published a demo where you can see both methods and play with them online (the parsing example is in app.js file - then click on the run button and see the result in the terminal):http://staging1.codefresh.io/labs/api/env/json-parse-example

you can modify the code and see the impact..

nathan gnathan g

Using JSON for your configuration with Node.js? Read this and get your configuration skills over 9000..

Note: People claiming that data = require('./data.json'); is a security risk and downvoting people's answers with zealous zeal: You're exactly and completely wrong. Try placing non-JSON in that file.. Node will give you an error, exactly like it would if you did the same thing with the much slower and harder to code manual file read and then subsequent JSON.parse(). Please stop spreading misinformation; you're hurting the world, not helping. Node was designed to allow this; it is not a security risk!

Proper applications come in 3+ layers of configuration:

  1. Server/Container config
  2. Application config
  3. (optional) Tenant/Community/Organization config
  4. User config

Most developers treat their server and app config as if it can change. It can't. You can layer changes from higher layers on top of each other, but you're modifying base requirements. Some things need to exist! Make your config act like it's immutable, because some of it basically is, just like your source code.

Failing to see that lots of your stuff isn't going to change after startup leads to anti-patterns like littering your config loading with try/catch blocks, and pretending you can continue without your properly setup application. You can't. If you can, that belongs in the community/user config layer, not the server/app config layer. You're just doing it wrong. The optional stuff should be layered on top when the application finishes it's bootstrap.

Stop banging your head against the wall: Your config should be ultra simple.

Take a look at how easy it is to setup something as complex as a protocol-agnostic and datasource-agnostic service framework using a simple json config file and simple app.js file..

container-config.js..

index.js.. (the engine that powers everything)

app.js.. (the code that powers your protocol-agnostic and.

Nick SteeleNick Steele
4,3752 gold badges21 silver badges23 bronze badges
eloyesp
1,7851 gold badge20 silver badges37 bronze badges
Ravindra GalavRavindra Galav
7362 gold badges8 silver badges16 bronze badges

Just want to complete the answer (as I struggled with it for a while), want to show how to access the json information, this example shows accessing Json Array:

EliEli

Just to make this as complicated as possible, and bring in as many packages as possible..

This lets you do:

Or if you're using async/await:

The advantage over just using readFileSync is that your Node server can process other requests while the file is being read off disk.

mpenmpen
133k188 gold badges667 silver badges994 bronze badges

JSON.parse will not ensure safety of json string you are parsing. You should look at a library like json-safe-parse or a similar library.

From json-safe-parse npm page:

JSON.parse is great, but it has one serious flaw in the context of JavaScript: it allows you to override inherited properties. This can become an issue if you are parsing JSON from an untrusted source (eg: a user), and calling functions on it you would expect to exist.

Timothy C. QuinnTimothy C. Quinn
1,0551 gold badge14 silver badges28 bronze badges

Leverage Lodash's attempt function to return an error object, which you can handle with the isError function.

l3xl3x
21.5k1 gold badge41 silver badges30 bronze badges

Always be sure to use JSON.parse in try catch block as node always throw an Unexpected Error if you have some corrupted data in your json so use this code instead of simple JSON.Parse

Rahul KambojRahul Kamboj

If you want to add some comments in your JSON and allow trailing commas you might want use below implemention:

Note that it might not work well if you have something like 'abc': 'foo // bar' in your JSON. So YMMV.

NuxNux
4,7943 gold badges41 silver badges53 bronze badges

If the JSON source file is pretty big, may want to consider the asynchronous route via native async / await approach with Node.js 8.0 as follows

Lae KettavongLae Kettavong

I use fs-extra. I like it a lot because -although it supports callbacks- it also supports Promises. So it just enables me to write my code in a much more readable way:

It also has many useful methods which do not come along with the standard fs module and, on top of that, it also bridges the methods from the native fs module and promisifies them.

NOTE: You can still use the native Node.js methods. They are promisified and copied over to fs-extra. See notes on fs.read() & fs.write()

So it's basically all advantages. I hope others find this useful.

Mig82Mig82

You can use JSON.parse() (which is a built in function that will probably force you to wrap it with try-catch statements).

Or use some JSON parsing npm library, something like json-parse-or

C'estLaVieC'estLaVie

Use JSON.parse(str);. Read more it here.

Here are some examples:

Praveen PooniaPraveen Poonia
adiga
20.6k7 gold badges30 silver badges52 bronze badges
Victor Michael KosgeiVictor Michael Kosgei

NodeJs is a JavaScript based server, so you can do the way you do that in pure JavaScript..

Imagine you have this Json in NodeJs..

And you can do above to get a parsed version of your json.. How to install apache with php on linux.

AlirezaAlireza
60.9k14 gold badges197 silver badges131 bronze badges

As mentioned in the above answers, We can use JSON.parse() to parse the strings to JSONBut before parsing, be sure to parse the correct data or else it might bring your whole application down

it is safe to use it like this

Shekar ManiaShekar Mania

No further modules need to be required.
Just use
var parsedObj = JSON.parse(yourObj);
I don think there is any security issues regarding this

Sachin SSachin S

It's simple, you can convert JSON to string using JSON.stringify(json_obj), and convert string to JSON using JSON.parse('your json string').

sampathsris
14.6k8 gold badges46 silver badges79 bronze badges
00imvj0000imvj00

This had to be shouted at me: it only works for .json files.

If the file ending is different this does not work!

SebastianSebastian
12 next

Not the answer you're looking for? Browse other questions tagged javascriptjsonnode.js or ask your own question.

Active4 years, 6 months ago

I am wondering how can I parse Array of JSON objects in NodeJS?

I want to post JSON array to the server, and be able to use the received array as a regualar JavaScript array.

Thanks in advance.

This is my front-end part that I am converting Array to String using stringify function

This my back-end part that I am trying to convert Array of JSON object to Array

This is Array that I am trying to sent to the server:

AphaApha
3601 gold badge9 silver badges28 bronze badges

3 Answers

In your app.js:

Then you can just use req.body to get the posted values:

Node Js Php Serialize Format Of Resume

In front-end, don't do any stringifying:

Vsevolod GolovizninVsevolod Goloviznin
10k1 gold badge30 silver badges41 bronze badges

Php Serialize Array

I'll try to explain this. First of all, you are crating a json string on the client.

Then on the server, you are doing the same again:

Then you parse the double stringifyed json string to a javascript object:

So now you actually have to parse it twice :). If you just stringify it on the server as you are now, and just call:

You will get a javascript object that you can access like this:

Have a look at this jsFiddle and open your developer tools. Look at the console when it runs and you will see where the problem is: example

cfscfs

You may actually send the JSON directly to server.

And in node.js, use bodyParser.json to get it back.

Php Serialize Json

By the way, do not use Array as variable name because Array represent the Array class used by JavaScript and your code has overwritten it.

Alex LauAlex Lau

Php 7 Vs Node Js

Not the answer you're looking for? Browse other questions tagged javascriptarraysjsonnode.js or ask your own question.