Player icon indicating copy to clipboard operation
Player copied to clipboard

New Command 2055 - Process JSON Code

Open jetrotal opened this issue 9 months ago • 9 comments

This command allows you to Get or Set values inside a JSON string. (Requires Maniacs Patch)

The syntax always comes in pairs. The first parameter of each pair indicates whether you are using a direct value or a variable/indirect variable, through ValueOrVariable().


Syntax (TPC):

@raw 2055,
      "",          // Path to the target value within the JSON data
      0, 0,        // Operation (0-1) -  0: Get (extract value), 1: Set (update value)
      0, 5,        // Source string variable ID containing the JSON data
      0, 2,        // Target variable Type (0-2) - 0: Switch, 1: Variable, 2: StringVariable
      0, 1,        // Target variable ID to receive data (for Get) or contain new value (for Set)
      0, 100,      // Usage of string variable instead of the path string.

Get Example:

  • Extract the value of "d" from the JSON data {"a":{"b":{"c":[{"d":"result"},"not_result"]}}} and store it in the string variable with ID 1.
@raw 2055,
      "a.b.c[0].d", // Path to the target value
      0, 0,         // Operation: Get
      0, 5,         // Source variable ID containing the JSON data
      0, 2,        // Target variable is a StringVar
      0, 1,         // Target variable ID to receive the extracted value
      0, 0          // Use literal path string

Set Example:

  • Update the value of "d" in the JSON data stored in the string variable with ID 5, using the value from the string variable with ID 2.
@raw 2055,
      "a.b.c[0].d", // Path to the target value
      1, 0,         // Operation: Set
      0, 5,         // Source variable ID containing the JSON data that will be modified
      0, 2,        // Target variable is a StringVar
      0, 2,         // Target variable ID containing the new value for "d"
      0, 0          // Use literal path string

This command works nice until you deal with a json string with more than 2mb: image

Every time I call it, the engine freezes for some frames. That may happen due to how I keep parsing and deleting entire JSON objects every time I call it. Maybe there's a way to keep it stored in memory for some time, to deal with multiple calls in sequence.

Here's a JSON file I used for testing bigger strings: JSONdatabase.txt

jetrotal avatar May 03 '24 04:05 jetrotal