home / skills / bdambrosio / cognitive_workbench / as-json

as-json skill

/src/tools_out/as-json

This skill identifies and extracts JSON from mixed content, optionally pulling specific fields to simplify data retrieval.

npx playbooks add skill bdambrosio/cognitive_workbench --skill as-json

Review the files below or copy the command above to add this skill to your agents.

Files (1)
Skill.md
2.7 KB
---
name: as-json
description: Identify and extract JSON from mixed content, optionally extracting specific fields
type: prompt_augmentation

---

# As JSON

Interpret note content as JSON, automatically stripping surrounding text, code fences, and other noise. Optionally extract specific fields.

## Purpose

- Extract JSON from LLM responses with preambles or explanations
- Strip markdown code fences (```json ... ```)
- Handle mixed content with embedded JSON
- Extract specific field values
- Validate JSON structure

## Input Format

Accepts a Note containing JSON (clean or embedded):
- Clean JSON: `{"key": "value"}`
- Code fenced: ` ```json\n{"key": "value"}\n``` `
- With preamble: `Here's the data:\n{"key": "value"}`
- With trailing text: `{"key": "value"}\nAs you can see...`

## Parameters

**Optional:**
- **field** - Field name to extract (searches recursively, e.g., "url", "title", "email")
- **all** - If true, returns all matching fields, one per line; if false/omitted, returns first match (default: false)

## Output Format

- **No field**: Full parsed JSON as formatted text
- **With field (all=false)**: First matching field value as text
- **With field (all=true)**: all matching field values, one per line
- **Field not found**: note-null
- **JSON not identified**: FAIL (hard error)

## Usage Examples

**Extract JSON from LLM response:**
{"type":"as-json","target":"$llm_response","out":"$parsed"}

**Extract first matching field:**
{"type":"as-json","target":"$response","args":{"field":"title"},"out":"$title"}

**Extract all matching fields:**
{"type":"as-json","target":"$response","args":{"field":"url","all":true},"out":"$all_urls"}

## Guidelines

- Strips leading/trailing text and code fences automatically
- Field search is recursive - finds field at any depth
- Field not found returns note-null (soft failure, plan continues)
- No valid JSON found triggers FAIL (hard failure, plan should handle)
- Preserves data types in extracted values
- Default extracts first match; use `all: true` for all matches

## Examples

**Extract first match:**
Input: `{"results": [{"url": "https://example.com/1"}, {"url": "https://example.com/2"}]}`  
field: "url"  
Output: `https://example.com/1`

**Extract all matches:**
Input: `{"results": [{"url": "https://example.com/1"}, {"url": "https://example.com/2"}]}`  
field: "url", all: true  
Output: `https://example.com/1\nhttps://example.com/2`

**Strip code fence:**
Input: `Here's the data:\n\`\`\`json\n{"count": 42}\n\`\`\``  
field: "count"  
Output: `42`

**Missing field:**
Input: `{"name": "Alice"}`  
field: "email"  
Output: `note-null`

Output ONLY the selected information as shown above, no introductory, explanatory, reasoning, code fences, etc.

Overview

This skill extracts valid JSON from mixed or noisy text and can optionally return specific field values. It strips surrounding prose, code fences, and other noise, then parses and validates the JSON. It can return the whole structure or targeted values, preserving data types.

How this skill works

The skill scans input for JSON structures, removes leading/trailing text and markdown code fences, and parses the first valid JSON blob it finds. If a field parameter is provided it searches the parsed JSON recursively and returns either the first match or all matches based on the all flag. It returns a hard FAIL when no valid JSON is detected and returns note-null when a requested field is not found.

When to use it

  • Cleaning LLM responses that include explanations around JSON payloads
  • Extracting a single value (e.g., url, title, email) from a JSON blob
  • Collecting all occurrences of a repeated field across nested arrays/objects
  • Validating that incoming text actually contains parseable JSON before further processing
  • Automating ingestion of JSON embedded in markdown or mixed content

Best practices

  • Pass only the text region likely to contain JSON to reduce false positives
  • Use field + all:true when you need every matching value instead of the first match
  • Rely on note-null to handle optional fields gracefully in downstream plans
  • Treat a FAIL as a hard error: add retry or fallback logic when JSON may be missing or malformed
  • Keep inputs small and focused to improve parsing reliability

Example use cases

  • Strip code-fenced JSON from an LLM reply and return the parsed object for downstream processing
  • Extract the first occurrence of "url" from API-like responses to collect a canonical link
  • Return all "email" fields from a nested contact list with all:true
  • Validate that a webhook payload contains JSON before sending it to a handler
  • Extract a numeric field like "count" from mixed text for metrics aggregation

FAQ

What happens if the input contains multiple JSON objects?

The skill finds and parses the first valid JSON blob it detects. Provide a focused input or pre-split text if you need multiple blobs.

How does the field search work?

Field lookup is recursive: it searches all nested objects and arrays. By default it returns the first match; set all:true to return every match, one per line.

What are the error signals?

If no valid JSON is identified the skill returns FAIL (hard error). If a field is not found in valid JSON it returns note-null (soft failure).