How to use parse-json - 8 common examples

To help you get started, we’ve selected a few parse-json examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mikelambert / dancedeets-monorepo / common / scripts / checkTutorials.js View on Github external
async function checkAllTutorials() {
  const files = (await fs.walk('../js/tutorials/playlists')).map(x => x.path);
  const jsonFiles = files.filter(x => x.endsWith('.json'));
  const promises = [];
  const tutorials = [];
  for (const jsonFilename of jsonFiles) {
    let jsonData = null;
    try {
      jsonData = parseJson(await fs.readFile(jsonFilename));
      tutorials.push(jsonData);
    } catch (e) {
      console.error('Error loading ', jsonFilename, '\n', e);
      continue;
    }
    promises.push(reportAndFix(jsonFilename, jsonData));
  }
  await Promise.all(promises);

  // Used for testing down below
  let defaultTutorials = null;
  try {
    defaultTutorials = require('../js/tutorials/playlistConfig.js').getTutorials(
      ''
    );
  } catch (e) {
github mikelambert / dancedeets-monorepo / scripts / checkTutorialsCore.js View on Github external
async function checkAllTutorials() {
  const files = await walk('js/learn/tutorials');
  const jsonFiles = files.filter((x) => x.endsWith('.json'));
  const promises = [];
  const tutorials = [];
  for (let jsonFilename of jsonFiles) {
    let jsonData = null;
    try {
      jsonData = parseJson(await readFile(jsonFilename));
      tutorials.push(jsonData);
    } catch (e) {
      console.error('Error loading ', jsonFilename, '\n', e);
      continue;
    }
    promises.push(reportAndFix(jsonFilename, jsonData));
  }
  await Promise.all(promises);

  // Used for testing down below
  let defaultTutorials = null;
  try {
    defaultTutorials = require('../js/learn/learnConfig.js').defaultTutorials;
  } catch (e) {
    console.error('Error importing learnConfig.js\n', e);
    return;
github zodern / meteor-up / src / plugin-api.js View on Github external
console.log(`Unable to load settings.json at ${filePath}`);
      if (e.code !== 'ENOENT') {
        console.log(e);
      } else {
        [
          'It does not exist.',
          '',
          'You can create the file with "mup init" or add the option',
          '"--settings path/to/settings.json" to load it from a',
          'different location.'
        ].forEach(text => console.log(text));
      }
      process.exit(1);
    }
    try {
      settings = parseJson(settings);
    } catch (e) {
      console.log('Error parsing settings file:');
      console.log(e.message);

      process.exit(1);
    }

    return settings;
  }
github zodern / meteor-up / src / plugin-api.js View on Github external
console.log(`Unable to load settings.json at ${filePath}`);
      if (e.code !== 'ENOENT') {
        console.log(e);
      } else {
        [
          'It does not exist.',
          '',
          'You can create the file with "mup init" or add the option',
          '"--settings path/to/settings.json" to load it from a',
          'different location.'
        ].forEach(text => console.log(text));
      }
      process.exit(1);
    }
    try {
      settings = parseJson(settings);
    } catch (e) {
      console.log('Error parsing settings file:');
      console.log(e.message);

      process.exit(1);
    }

    return settings;
  }
github patternplate / patternplate / packages / server / source / library / utilities / load-json.js View on Github external
async function loadJson(file) {
  const contents = String(await sander.readFile(file));
  try {
    return [null, parse(contents, file)];
  } catch (error) {
    const message = error.message;
    const [numbers] = /(\d+:\d+)/.exec(message) || [""];

    const [line = 0, column = 0] = numbers
      .split(":")
      .map(n => Number(n))
      .filter(n => !isNaN(n));

    const frame = codeFrame(contents, line, column);

    const err = new Error(["", frame, message].join("\n"));
    err.filename = file;
    return [err];
  }
}
github rpl / flow-coverage-report / src / lib / cli / config.js View on Github external
...args
    };
  }

  if (args.includeGlob) {
    args.includeGlob = toArray(args.includeGlob);
  }

  if (args.globIncludePatterns) {
    args.globIncludePatterns = toArray(args.globIncludePatterns);
  }

  if (args.config) {
    const filePath = path.resolve(args.config);
    const fileRawData = fs.readFileSync(filePath);
    const fileConfigData = parseJSON(stripJSONComments(String(fileRawData)));

    if (process.env.VERBOSE) {
      console.log('Loaded config from file', filePath, fileConfigData);
    }

    return {
      ...defaultConfig,
      ...normalizedConfig(fileConfigData),
      ...args
    };
  }

  let packageJSONPath;

  try {
    packageJSONPath = path.resolve(path.join(getProjectDir(args), 'package.json'));
github mozilla / web-ext / src / cmd / build.js View on Github external
let messageData: LocalizedMessageData;
  let messageContents: string | Buffer;
  let extensionName: string = manifestData.name;

  try {
    messageContents = await fs.readFile(messageFile, {encoding: 'utf-8'});
  } catch (error) {
    throw new UsageError(
      `Error reading messages.json file at ${messageFile}: ${error}`);
  }

  messageContents = stripBom(messageContents);

  try {
    messageData = parseJSON(stripJsonComments(messageContents));
  } catch (error) {
    throw new UsageError(
      `Error parsing messages.json file at ${messageFile}: ${error}`);
  }

  extensionName = manifestData.name.replace(
    /__MSG_([A-Za-z0-9@_]+?)__/g,
    (match, messageName) => {
      if (!(messageData[messageName]
            && messageData[messageName].message)) {
        const error = new UsageError(
          `The locale file ${messageFile} ` +
            `is missing key: ${messageName}`);
        throw error;
      } else {
        return messageData[messageName].message;
github boltpkg / bolt / src / utils / config.js View on Github external
export async function readConfigFile(filePath: string): Promise {
  let contents = await fs.readFile(filePath);
  let config = parseJson(contents);
  return config;
}

parse-json

Parse JSON with more helpful errors

MIT
Latest version published 5 months ago

Package Health Score

80 / 100
Full package analysis

Popular parse-json functions