json-parser-yaml-converter
Version:
Enhanced JSON Parser with verbose error messages and JSON to YAML conversion
57 lines (52 loc) • 2 kB
Markdown
- Juan Rodríguez Suárez
- alu0101477596@ull.edu.es
[](https://opensource.org/licenses/ISC)
A JSON Parser which tells you if the JSON is bad formatted or not. If it is, it will tell you which line has the error and what was expected.
- **Example on CLI**:
```bash
npx checkjson jsonFile.json
```
Will show you the error if there is one, or it will just tell you if the JSON is well formatted.
- **Example on Node**:
```js
import { jsonParse } from '@ull-esit-pl-2324/tfa-juan-rodriguez-suarez-alu0101477596';
let obj = jsonParse('jsonFile.json');
```
If it fails, it will throw an error with the line, column, the expected tokens and the token that was found:
```js
try {
let obj = jsonParse('jsonFile.json');
} catch (error) {
console.log(error.token, error.expected, error.line, error.column);
}
```
A JSON to YAML converter that will convert a JSON file to a YAML file.
- **Example on CLI**:
```bash
npx json2yaml jsonFile.json
```
Will create a YAML file with the same name as the JSON file but with the .yaml extension.
With the `-o` flag you can specify the output file:
```bash
npx json2yaml jsonFile.json -o yamlFile.yaml
```
Will create a YAML file with the name `yamlFile.yaml`.
**In both cases, if the JSON file is bad formatted, it will throw an error similar to the one in the JSON Parser.**
- **Example on Node**:
```js
import { jsonToYaml } from '@ull-esit-pl-2324/tfa-juan-rodriguez-suarez-alu0101477596';
jsonToYaml('jsonFile.json', 'yamlFile.yaml');
```
If it fails, it will throw an error similar to the one in the JSON Parser.
A function that will look for a key in a JSON file and return the value of that key.
- **Example on CLI**:
```bash
npx searchkey jsonFile.json key
```
Will print the value of **all** the keys matching the given key and its value.