JSON Tools

Cleanse and filter JSON for later use.

{{ jsonTools  | filter: "value" | filterType: "" }}

Parameters:

  • Filter: A value for which we should filter items.
  • Filter Type: If set to Equals, filter for objects/keys where the value equals the value set in filter. If set to Not Equals, filter for objects/keys where the value does not equal the value set in filter.

Returns: The resulting JSON.

Example:

{
  "books": [
 {
      "title": "The Great Gatsby",
      "summary": "The Great Gatsby is a novel by F. Scott Fitzgerald that follows the story of Jay Gatsby, a wealthy and mysterious man, and his pursuit of the American Dream. Set in the 1920s, the book explores themes of love, wealth, and the corruption of the American Dream.",
      "author": "F. Scott Fitzgerald"
 }
 ]
}
{{ jsonTools  | filter: "The Great Gatsby" | filterType: "Equals" }}

Would yield:

{
  "books": [
 {
      "title": "The Great Gatsby"
 }
 ]
}

Where setting filterType to Not Equals would yield:

{
  "books": [
 {
      "summary": "The Great Gatsby is a novel by F. Scott Fitzgerald that follows the story of Jay Gatsby, a wealthy and mysterious man, and his pursuit of the American Dream. Set in the 1920s, the book explores themes of love, wealth, and the corruption of the American Dream.",
      "author": "F. Scott Fitzgerald"
 }
 ]
}

ReMap JSON

Remap elements in a JSON object from one key name to another.

{{ reMapJSON  | match: "" | replace: "" }}

Parameters:

  • Match: A variable that you are aiming to replace.
  • Replace: The variable that will replace all instances of the variable set as your Match parameter.

Returns: Changes the key name of a variable to the name used in the Replace parameter.

Example:

{
  "books": [
 {
      "title": "The Great Gatsby",
      "summary": "The Great Gatsby is a novel by F. Scott Fitzgerald that follows the story of Jay Gatsby, a wealthy and mysterious man, and his pursuit of the American Dream. Set in the 1920s, the book explores themes of love, wealth, and the corruption of the American Dream.",
      "author": "F. Scott Fitzgerald"
 }
 ]
}
{{ reMapJSON  | match: "The Great Gatsby" | replace: "To Kill a Mockingbird" }}

Would yield:

{
  "books": [
 {
      "title": "To Kill a Mockingbird",
      "summary": "The Great Gatsby is a novel by F. Scott Fitzgerald that follows the story of Jay Gatsby, a wealthy and mysterious man, and his pursuit of the American Dream. Set in the 1920s, the book explores themes of love, wealth, and the corruption of the American Dream.",
      "author": "F. Scott Fitzgerald"
 }
 ]
}

JSON Array Filter

Filter a JSON array

{{ arrayFilter  | filter: "" | filterType: "" }}

Parameters:

  • Filter: A value for which we should filter items.
  • Filter Type: There are four different index types:
  1. Index: You can use a filterType of "Index" and pass the numerical index of the array to return.

  2. IndexRange: IndexRange expects number-number of indexes to extract, E.G.: 1-3.

  3. Value Match: Value match and value Contains will filter the array by finding objects in the array with properties that match the filter value.

  4. Value Contains: Value match and value Contains will filter the array by finding objects in the array with properties that match the filter value.

JSON Key Filter

Filter a JSON Object by a list of keys.

{{ keyFilter  | filter: "" }}

Parameters:

  • Filter: A comma-separated list of keys to filter a JSON object by.

Returns: The JSON object, filtered by the keys inputted in the parameters.

Example:

 {
      "title": "The Great Gatsby",
      "summary": "The Great Gatsby is a novel by F. Scott Fitzgerald that follows the story of Jay Gatsby, a wealthy and mysterious man, and his pursuit of the American Dream. Set in the 1920s, the book explores themes of love, wealth, and the corruption of the American Dream.",
      "author": "F. Scott Fitzgerald"
 }
{{ keyFilter  | filter: "title, author" }}

Would yield:

{"title":"The Great Gatsby","author":"F. Scott Fitzgerald"}

JSON to Variables

Accepts JSON as an input, flattens the object keys, and sets those keys as variables in mAIstro's context.

Parameters: None - Data should be "chained" into this function.

Returns: None - Variables are assigned as a result of this function.

Example: Data can come from a LLM, a file, REST API response, etc

{{ LLM  | prompt: "Output some information about a book in JSON format. Include title, summary, and author." | modelCard: "" }}=>{{ jsonToVars }}

The output from the LLM:

{
  "title": "The Great Gatsby",
  "summary": "The Great Gatsby is a novel by F. Scott Fitzgerald that follows the story of Jay Gatsby, a wealthy and mysterious man, and his pursuit of the American Dream. Set in the 1920s, the book explores themes of love, wealth, and the corruption of the American Dream.",
  "author": "F. Scott Fitzgerald"
}

Finally, looking in the variable inspector, you can see the variables now set available for use:

variable inspector

Variables to JSON

Convert environment variables into JSON.

{{ varsToJSON  | path: "" | variable: "" }}

Parameters:

  • Path: (Optional) The flattened path from which to start obtaining values.
  • Variable: The name of the variable to assign the resulting JSON.

Returns: Nothing - Output is assigned to the set variable name.

Example 1:

Howard has 20 cats and 40 dogs. 
He took them to the vet last week.=>{{ grammar }}=>{{ variable | name: "text" }}
{{ varsToJSON  | path: "" | variable: "gm" }}
<< name: gm, prompt: false >>

Will yield:

{
 "grammar": {
 "year": [
 2024
 ],
 "context": "",
 "dates": [
 "last",
 "week"
 ],
 "propernouns": [
 "Howard"
 ],
 "nouns": [
 "20 cats",
 "40 dogs",
 "vet",
 "week"
 ],
 "preps": [
 "He",
 "them"
 ],
 "determiners": []
 },
 "text": "Howard has 20 cats and 40 dogs. \nHe took them to the vet last week."
}

Example 2: Using the path parameter, we can specify the starting path of values we want:

Howard has 20 cats and 40 dogs. 
He took them to the vet last week.=>{{ grammar }}=>{{ variable | name: "text" }}
{{ varsToJSON  | path: "grammar.dates" | variable: "gm" }}
<< name: gm, prompt: false >>

Will yield:

{
 "grammar": {
 "dates": [
 "last",
 "week"
 ]
 }
}

JSON Escape

Escapes a string for use within a JSON object.

{{ jsonEscape  }}

Parameters: None - Data should be "chained" into this function.

Returns: An escaped JSON string, which removes special characters to make parsing and storing easier.

Example:

{
  "books": [
 {
      "title": "The Great Gatsby",
      "summary": "The Great Gatsby is a novel by F. Scott Fitzgerald that follows the story of Jay Gatsby, a wealthy and mysterious man, and his pursuit of the American Dream. Set in the 1920s, the book explores themes of love, wealth, and the corruption of the American Dream.",
      "author": "F. Scott Fitzgerald"
 }
 ]
}
{{ jsonEscape }}

Would yield:

{\n \"books\": [\n {\n \"title\": \"The Great Gatsby\",\n \"summary\": \"The Great Gatsby is a novel by F. Scott Fitzgerald that follows the story of Jay Gatsby, a wealthy and mysterious man, and his pursuit of the American Dream. Set in the 1920s, the book explores themes of love, wealth, and the corruption of the American Dream.\",\n \"author\": \"F. Scott Fitzgerald\"\n }\n ]\n}\n

Ⓒ 2024 NeuralSeek, all rights reserved.