Building a JSON object in code

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Building a JSON object in code

Post by ziltmilt »

If your macro collects values for various fields, how would you then collect and store those fields into a new JSON object? json.merge looks like it might be the answer, except that its arguments are all pre-existing JSON objects.

I suppose I could concatenate and build the object via something like this (if there's an equivalent to the chr function)

Code: Select all

"{" + chr(34) + fieldName1 + chr(34) + ":" + etc etc

Any ideas? thanks in advance!

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Building a JSON object in code

Post by aliasmask »

json.set will build a json object and json.append builds a json array. You want to avoid trying to do it manually.

Aurien Eleneill
Kobold
Posts: 6
Joined: Sat Jan 16, 2016 11:14 pm

Re: Building a JSON object in code

Post by Aurien Eleneill »

Also, it look like your JSON object's key will be "built" out of pieces?
while you can just set a variable to hold those, strformat tends to be cleaner, with less bugs, and a set of useful manipulation abilities.
Very useful if there's any level of uncertainty in the input.

[keystring = strformat("text %{value1} more text etc. %{value2}")]
[newObject = json.set("{}",keystring,someOtherValueOrString)]

if the JSON exists already replace "{}" with either "actualJsonName" or variableRepresentingAJson

Hopefully I typed that all right haha I usually miss a parantheses or something ;)

Post Reply

Return to “Macros”