UNPKG

json-parser-yaml-converter

Version:
57 lines (52 loc) 2 kB
# TFA: Better JSON Parser & JSON to YAML Converter ## Author - Juan Rodríguez Suárez - alu0101477596@ull.edu.es ## License [ISC](https://opensource.org/licenses/ISC) # Better JSON Parser 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); } ``` # JSON to YAML Converter 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. # Look for keys in JSON 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.