### Streaming JSON Parsing Example in JavaScript Source: https://github.com/karminski/streaming-json-js/blob/main/README.md This JavaScript example demonstrates the core functionality of streaming-json-js. It shows how to initialize a Lexer, append JSON segments, and complete the JSON string. The library handles incomplete or fragmented JSON by completing it to a syntactically correct form. ```javascript // init, @NOTE: We need to assign a new lexer for each JSON stream. lexer = new streamingjson.Lexer(); // append your JSON segment lexer.AppendString(`{"a":`); // complete the JSON console.log(lexer.CompleteJSON()); // will print `{"a":null}` // append more JSON segment lexer.AppendString(`[tr`); // complete the JSON again console.log(lexer.CompleteJSON()); // will print `{"a":[true]}` ``` -------------------------------- ### Install streaming-json-js via npm Source: https://github.com/karminski/streaming-json-js/blob/main/README.md This command installs the streaming-json library using npm, a package manager for Node.js. This is a prerequisite for using the library in your Node.js projects. ```bash npm install --save streaming-json ``` -------------------------------- ### Append JSON Fragments and Parse Incrementally - JavaScript Source: https://context7.com/karminski/streaming-json-js/llms.txt Shows how to use the AppendString method to process a series of JSON fragments, calling CompleteJSON() after each fragment to get a valid JSON string. The code demonstrates parsing the JSON at each step, highlighting the library's ability to provide parsable JSON even with incomplete streams. This is beneficial for real-time updates. ```javascript const streamingjson = require('streaming-json'); const lexer = new streamingjson.Lexer(); // Handle streaming data from an API or LLM const fragments = ['{"user', 's": [{"id":', ' 1, "na', 'me": "Alice"', '}, {"id": 2']; fragments.forEach(fragment => { lexer.AppendString(fragment); // Get valid JSON at each step const current = lexer.CompleteJSON(); console.log(current); // Safe to parse at any point try { const obj = JSON.parse(current); console.log('Successfully parsed:', obj); } catch (e) { console.error('Parse error:', e.message); } }); // Output progression: // {"user":null} // {"users": [{"id": 1}]} // {"users": [{"id": 1, "na":null}]} // {"users": [{"id": 1, "name": "Alice"}]} // {"users": [{"id": 1, "name": "Alice"}, {"id": 2}]} ``` -------------------------------- ### Instantiate and Use Lexer to Parse Streaming JSON - JavaScript Source: https://context7.com/karminski/streaming-json-js/llms.txt Demonstrates how to instantiate the Lexer class, append JSON fragments as they arrive, and obtain a complete, valid JSON string using the CompleteJSON() method. The completed JSON can then be parsed using standard JSON.parse. This is useful for handling incremental data from various sources. ```javascript const streamingjson = require('streaming-json'); // Create a new lexer instance for each JSON stream const lexer = new streamingjson.Lexer(); // Append incomplete JSON fragments as they arrive lexer.AppendString('{"name": "John", "age": 30, "address": {"city": "New'); // Get a complete, valid JSON string const completedJSON = lexer.CompleteJSON(); console.log(completedJSON); // Output: {"name": "John", "age": 30, "address": {"city": "New"}} // Continue appending more data to the same lexer lexer.AppendString(' York", "zip": "10001"}'); const finalJSON = lexer.CompleteJSON(); console.log(finalJSON); // Output: {"name": "John", "age": 30, "address": {"city": "New York", "zip": "10001"}} // Parse the completed JSON with standard JSON.parse const parsed = JSON.parse(finalJSON); console.log(parsed.address.city); // "New York" ``` -------------------------------- ### Parse JSON Fragments with streaming-json-js Lexer Source: https://github.com/karminski/streaming-json-js/blob/main/examples/gpt-function-call/demo-global.html This snippet demonstrates the core functionality of the streaming-json-js library. It initializes a Lexer and appends string fragments, showing how the `CompleteJSON()` method progressively parses the input into valid JSON. This is useful for handling incomplete JSON data received over a network stream. ```javascript const arguments = ['{\"fu', 'nction', '\'_name', '\"', ':', '\"run', '\'_code', '\", ', '\"argu', 'ments\"', ': ', '\"print(', '\\\"hello', ' world', '\\\"', '\")\"']; const lexer = new window['streaming-json'].Lexer(); arguments.forEach(jsonFragment => { lexer.AppendString(jsonFragment); console.log(lexer.CompleteJSON()); }); ``` -------------------------------- ### Complete Incomplete JSON Stream for GPT Function Calls - JavaScript Source: https://context7.com/karminski/streaming-json-js/llms.txt Illustrates using the CompleteJSON method to handle streaming responses from services like GPT, specifically for function call arguments. The code simulates receiving fragmented arguments and uses CompleteJSON() to form valid JSON at each step, allowing for real-time parsing and extraction of function names and arguments. This method is crucial for processing structured data as it arrives. ```javascript const streamingjson = require('streaming-json'); // Example: Handling GPT function call streaming const lexer = new streamingjson.Lexer(); // Simulating GPT streaming response for tool_calls arguments const argumentFragments = [ '{"fu', 'nction', '_name', '": "', 'run_code', '", "ar', 'guments": "', 'print(\"hello', ' world\"', ')"' ]; argumentFragments.forEach((fragment, index) => { lexer.AppendString(fragment); const completed = lexer.CompleteJSON(); console.log(`Step ${index + 1}:`, completed); // Parse and extract data in real-time const parsed = JSON.parse(completed); if (parsed.function_name) { console.log(` -> Function: ${parsed.function_name}`); } if (parsed.arguments) { console.log(` -> Arguments: ${parsed.arguments}`); } }); // Output: // Step 1: {"fu":null} // Step 2: {"function":null} // Step 3: {"function_name":null} // Step 4: {"function_name": ""} // Step 5: {"function_name": "run_code"} // -> Function: run_code // Step 6: {"function_name": "run_code", "ar":null} // -> Function: run_code // Step 7: {"function_name": "run_code", "arguments": ""} // -> Function: run_code // -> Arguments: // Step 8: {"function_name": "run_code", "arguments": "print(\"hello"} // -> Function: run_code // -> Arguments: print("hello // Step 9: {"function_name": "run_code", "arguments": "print(\"hello world\""} // -> Function: run_code // -> Arguments: print("hello world" // Step 10: {"function_name": "run_code", "arguments": "print(\"hello world\")"} // -> Function: run_code // -> Arguments: print("hello world") ``` -------------------------------- ### Import streaming-json-js in Node.js Source: https://github.com/karminski/streaming-json-js/blob/main/README.md This code snippet demonstrates how to import the streaming-json library in a Node.js environment using the require function. This is the standard way to include external modules in Node.js. ```javascript const streamingjson = require('streaming-json'); ``` -------------------------------- ### Process Fragmented JSON with streaming-json-js Lexer Source: https://github.com/karminski/streaming-json-js/blob/main/examples/gpt-function-call/demo-amd.html This snippet demonstrates how to use the streaming-json-js Lexer to process fragmented JSON strings, simulating a scenario where JSON data arrives in chunks. It appends each fragment and prints the progressively completed JSON. ```javascript require(['../../dist/index.aio.js'], function (streamingjson) { // In GPT's chat completion stream mode, the request for tool_calls returns a structure as follows: // // { // "id": "chatcmpl-?", // "object": "chat.completion.chunk", // "created": 1712000001, // "model": "gpt-4-0125-preview", // "system_fingerprint": "fp_?", // "choices": [ // { // "index": 0, // "delta": { // "tool_calls": [ // { // "index": 0, // "function": { // "arguments": "{\"fi" // } // } // ] // }, // "logprobs": null, // "finish_reason": null // } // ] // } // // We need extract data.choices[0].delta.tool_calls[0].function.arguments. // The arguments fiels is a JSON fragment, we can use steaming-json-go complete it to a syntactically correct JSON and Unmarshal it. // We use string slice to simulate the arguments field in the return of GPT. let arguments = ['{"fu', 'nction', '_name', '"', ':', '"run', '_code', '", ', '"argu', 'ments"', ': ', '"print(', '\\"hello', ' world', '\\"', ')"']; let lexer = new streamingjson.Lexer(); arguments.forEach(jsonFragment => { lexer.AppendString(jsonFragment); console.log(lexer.CompleteJSON()); }); }); ``` -------------------------------- ### Include streaming-json-js in Browser Source: https://github.com/karminski/streaming-json-js/blob/main/README.md This HTML script tag demonstrates how to include the streaming-json library in a browser environment by referencing its UMD (Universal Module Definition) build. This makes the library available globally. ```html ``` -------------------------------- ### Import streaming-json-js in Webpack/Rollup Source: https://github.com/karminski/streaming-json-js/blob/main/README.md This import statement shows how to use streaming-json with module bundlers like Webpack or Rollup, which are common in modern JavaScript development for managing dependencies and creating optimized bundles. ```javascript import streamingjson from 'streaming-json'; ``` -------------------------------- ### JavaScript: Handle JSON Edge Cases with Lexer Source: https://context7.com/karminski/streaming-json-js/llms.txt Demonstrates how the streaming-json-js Lexer handles incomplete JSON strings, including escape characters, unicode, numbers (scientific notation), booleans, null values, nested structures, and trailing commas. It processes data in chunks and provides complete JSON objects when available. ```javascript const streamingjson = require('streaming-json'); // Test case 1: Incomplete strings with escape characters const lexer1 = new streamingjson.Lexer(); lexer1.AppendString('{"message": "Line 1\nLine 2\tTabbed\u00'); console.log(lexer1.CompleteJSON()); // Output: {"message": "Line 1\nLine 2\tTabbed"} // Test case 2: Incomplete numbers (including scientific notation) const lexer2 = new streamingjson.Lexer(); lexer2.AppendString('{"values": [42, -3.14, 2.998e'); console.log(lexer2.CompleteJSON()); // Output: {"values": [42, -3.14, 2.998]} // Test case 3: Incomplete boolean and null values const lexer3 = new streamingjson.Lexer(); lexer3.AppendString('{"active": tr, "deleted": fal, "metadata": nu'); console.log(lexer3.CompleteJSON()); // Output: {"active": true, "deleted": false, "metadata": null} // Test case 4: Deeply nested structures const lexer4 = new streamingjson.Lexer(); lexer4.AppendString('{"data": {"users": [{"id": 1, "profile": {"name": "Al'); console.log(lexer4.CompleteJSON()); // Output: {"data": {"users": [{"id": 1, "profile": {"name": "Al"}}]}} // Test case 5: Trailing commas const lexer5 = new streamingjson.Lexer(); lexer5.AppendString('{"items": [1, 2, 3,'); console.log(lexer5.CompleteJSON()); // Output: {"items": [1, 2, 3]} // Test case 6: Arrays and objects as values const lexer6 = new streamingjson.Lexer(); lexer6.AppendString('{"config": {"enabled": true, "options": ['); console.log(lexer6.CompleteJSON()); // Output: {"config": {"enabled": true, "options": []}} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.