aks
aks

Reputation: 9491

When to use a json file and when to use a js file?

I am trying to do some declarative programming in javascript, where I create a spec and a chart should be rendered based on the spec. I was wondering if I should store the spec which defines the chart in a json file or a js file?

Are there any best practices for doing this?

I was also wondering(out of curiosity) why package.json is not a js file?

Upvotes: 0

Views: 59

Answers (1)

RustyB
RustyB

Reputation: 147

At the risk of stating the obvious, a .js file contains javascript code. A .json file contains a JSON data structure, made up of a hierarchy of name:value pairs. See: http://www.json.org/

I can't comment on your use case without further information, particularly regarding the contents of your 'spec'; but if it contains a JSON data structure, it would be a .json file.

package.json is a great example. The structure is JSON format, so it's a .json file. It is made up of a set of name:value pairs describing the package dependencies of a .js program. See: https://docs.npmjs.com/files/package.json

When creating and testing your spec, this will be a useful tool: https://jsonlint.com/

Upvotes: 1

Related Questions