hasura/graphql-engine · error

parsing metadata as json: %w

Error message

parsing metadata as json: %w

What it means

After any YAML-to-JSON conversion, `metadata apply` unmarshals the metadata bytes with json.Unmarshal into a generic `any`. This error means the (converted) bytes are not valid JSON — the local metadata file is corrupt at the JSON level, or the YAML conversion produced something unparseable.

Source

Thrown at cli/commands/metadata_handlers.go:381

		if err != nil {
			return errors.E(op, fmt.Errorf("displaying metadata failed: %w", err))
		}

		return nil
	}

	if mode == cli.MetadataModeYAML {
		localMetadataBytes, err = metadatautil.YAMLToJSON(localMetadataBytes)
		if err != nil {
			return errors.E(op, fmt.Errorf("parsing yaml metadata to json: %w", err))
		}
	}

	var metadata any

	err = json.Unmarshal(localMetadataBytes, &metadata)
	if err != nil {
		return errors.E(op, fmt.Errorf("parsing metadata as json: %w", err))
	}

	if o.EC.Config.Version == cli.V2 {
		_, err := cli.GetCommonMetadataOps(o.EC).
			ReplaceMetadata(bytes.NewReader(localMetadataBytes))
		if err != nil {
			return errors.E(op, err)
		}

		return nil
	}

	r, err := o.EC.APIClient.V1Metadata.V2ReplaceMetadata(hasura.V2ReplaceMetadataArgs{
		AllowInconsistentMetadata: !o.DisallowInconsistencies,
		Metadata:                  metadata,
	})
	if err != nil {
		return errors.E(op, errorApplyingMetadata(err))

View on GitHub (pinned to 724551b9ae)

Solutions

  1. Run jq . metadata.json to get the exact parse error location
  2. Remove comments/trailing commas, ensure standard JSON syntax
  3. Re-export metadata from the server to restore a known-good file, then re-apply changes
  4. Ensure the file is saved as UTF-8 without BOM and is non-empty

Example fix

# before
hasura metadata apply  # parsing metadata as json: invalid character ',' ...
# after
jq . metadata.json  # fix trailing comma, then
hasura metadata apply
Defensive patterns

Strategy: validation

Validate before calling

data, _ := os.ReadFile(ec.MetadataFile)
if len(data) == 0 { return errors.New("metadata file empty") }
var v any
if err := json.Unmarshal(data, &v); err != nil {
  return fmt.Errorf("metadata file invalid JSON: %w", err)
}

Try / catch

if err := apply(o, mode); err != nil {
  if strings.Contains(err.Error(), "parsing metadata as json") {
    // jq . metadata.json to find the exact syntax problem
  }
}

Prevention

When it happens

Trigger: `metadata apply` where json.Unmarshal of the metadata file bytes fails: invalid JSON syntax (trailing commas, comments, single quotes), empty file, BOM prefix, or truncated file from a bad export or edit.

Common situations: Hand-edited metadata.json with comments or trailing commas (not valid JSON); file truncated by an interrupted write; file saved with UTF-8 BOM by an editor; empty file created accidentally.

Related errors


AI-assisted analysis of hasura/graphql-engine@724551b9ae (2026-08-28). Data as JSON: /api/errors/7d61a63c32efebde. Report an issue: GitHub.