How do you add items to a JSON Array?

NOTE: I want to know how to do this with KM Actions, not JavaScript (I already know how to do that).

Let's say I have the following JSON array, which I've simplified for this question:

[ { "name": "Fred" } ]

And a JSON item, like this:

{ "name": "Dan" }

I want to add the new item to the JSON array. The only way I can figure out how to do it is like this - first in pseudo-code:

  • set Local_Index to the length of the JSON array
  • add 1 to Local_Index
  • set the JSON value using Local_Index

now the KM actions:

image

 
There's got to be an easier way!

Click to see the entire macro

image

 
Here's the macro source:

Append to a JSON Array.kmmacros (4.1 KB)

You can combine those three actions into one:

image

Append to a JSON Array (compact).kmmacros (3.2 KB)

Image

...the extra maths being required because of KM's 1-based array indexing.

Otherwise, isn't JSON just text in a standardised format? In which case:

image

Append to a JSON Array (strings).kmmacros (3.2 KB)

Image

Both should also work with "internal" arrays if you've a nested format -- in both cases you'd be pulling the "internal" array into Local_Array, manipulating it, then replacing the original in the blob.

2 Likes

Awesome - thanks!

Thwap! (Sound of hand hitting forehead) Duh! I should have thought of that.

Thanks!!

1 Like

You don't prefer an MVC simplification which:

  1. uses JS for the model, and
  2. KM for the reporting and interfaces ?

Data manipulations by string-munging seem a bit complex to this old mind :slight_smile:

"MVC" Ugh, shiver. Don't remind me of my professional development days.

Yes, I do prefer using JS/JXA. But I'm willing to bet that doing it in KM actions, when it's simple enough, is faster (execution-wise). Strike that - I'm not willing to bet, because that's like throwing the gauntlet down, and we've both got better things to do with out time, right? ;p

Fortunately, I found a better way to do what I wanted to do, so I don't even need this now. But it's good to know how to do it, anyway.

1 Like

Especially for those of us who might want to use JSON for passing data around our macros but get hives as soon as they see scripts starting with return (() => {...

And while I certainly wouldn't recommend the Search and Replace route it's a handy reminder that this is just text. S'n'R may be the hammer in the JSON manipulation toolbox, and it's better to use the right screwdriver if you can -- but sometimes you just need to hit things :wink:

Or perhaps the clef anglaise ?

( French attributes it to the English – I understand that the Anglo Saxons attribute it elsewhere )


( re hammers, few things are actually nails :slight_smile: )


Simplest JS script is just:

return 2 + 2

( the rest just elaborates that expression )

And screws are just nails that need hitting a bit harder...

1 Like

Personally, I think text manipulation is underutilized. Case in point:

I need to put a graphic image into a KM action. I have no idea how to do that, using a JXA Plist object. But I can manipulate the XML for the action, and stick a base64 encoded image into the XML. Problem solved.

(I'm assuming this works. If not, I'll let you know later today.)

(And this is assuming that someone of virtually inexhaustible knowledge doesn't show me how to do it using Obj-C/JXA. Hint, hint, @ComplexPoint.)

Could you unpack that with a bit of context ?

( not quite sure what it means – presumably not the kind of case in which a custom HTML action might display some SVG or a png file ?)

Perhaps in a separate thread ?