Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219-thinking-32k· AnthropicHugging Face LMArena Human Preference 140K

get json using SAX

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    get json using SAX

  2. Claude Assistant

    For a SAX-like approach to parse and extract JSON from text, we can use a streaming JSON parser like clarinet. This provides an event-based parsing method similar to SAX for XML. Here's how to implement it:

    const clarinet = require('clarinet');
    const fs = require('fs');
    const { StringDecoder } = require('string_decoder');
    
    // Function to extract JSON from text with markdown code blocks
    function extractJsonFromText(text) {
      // Find the boundaries of the JSON block
      const startMarker = '```json\n';
      const endMarker = '\n```';
      
      const startIndex = text.indexOf(startMarker);
      if (startIndex === -1) return null;
      
      const jsonStartIndex = startIndex + startMarker.length;
      const endIndex = text.indexOf(endMarker, jsonStartIndex);
      if (endIndex === -1) return null;
      
      // Extract the JSON text
      const jsonText = text.substring(jsonStartIndex, endIndex);
      
      // Parse the JSON using clarinet (SAX-like parser)
      const parser = clarinet.parser();
      const decoder = new StringDecoder('utf8');
      
      let result = null;
      let stack = [];
      let currentKey = null;
      let currentObject = null;
      
      parser.onopenobject = function(key) {
        const newObj = {};
        if (currentObject === null) {
          // This is the root object
          currentObject = newObj;
          result = newObj;
        } else {
          // Nested object
          if (currentKey !== null) {
            currentObject[currentKey] = newObj;
          }
          stack.push({ obj: currentObject, key: currentKey });
          currentObject = newObj;
        }
        currentKey = key || null;
      };
      
      parser.onkey = function(key) {
        currentKey = key;
      };
      
      parser.onvalue = function(value) {
        if (currentKey !== null) {
          currentObject[currentKey] = value;
          currentKey = null;
        }
      };
      
      parser.oncloseobject = function() {
        if (stack.length > 0) {
          const parent = stack.pop();
          currentObject = parent.obj;
          currentKey = parent.key;
        }
      };
      
      // Feed the JSON text to the parser
      parser.write(decoder.write(jsonText));
      parser.close();
      
      return result;
    }
    
    // Example usage
    const text = `fewfwefewfewf
    fwefewfewf
    \`\`\`json
    {
      "a": 1
    }
    \`\`\``;
    
    const extractedJson = extractJsonFromText(text);
    console.log("Extracted JSON:", extractedJson);
    

    Would you like me to explain how this code works?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.