How to fix "JSON parse error: Unrecognized token '/'" by reading JSON from Filepath defined in KM Variable?

I am currently working on one of my Spotlight Search Prompt based Projects which uses the dataType JSON.
I decided to use a .pkg (Package-) File for all of them to store all Resources in a Subfolder structure inside the Package based on the data providing Macro.

The Package is stored in my Dropbox for sync between my Macs and defined in a Global Variable called DND__sspMacroResourcesPackagePath .

For each of my "SSP - Main Macros" I use this Variable for building a Source based on local Variables - like so:

The magenta colored Variable Local__sspDataFile holds the Path for the JSON File inside the Package based on the Macro.
In this Case the Path inside the Package is:

Resources/KMF_Topics/sspData_JSON/SSP_Keyboard Maestro Discourse - Topics.json

The Variable Local__sspDataFullPath then uses the Variable DND__sspMacroResourcesPackagePath & the Variable Local__sspDataFile for building the full Resource for the Variable sspData .

The full path in this Case is:

/Users/kurty_309/Dropbox/Scripting/Dev/Keyboard Maestro/Macro_Resources/@Nr5_need_Input/SSP Macro Resource Files Package.pkg/Resources/KMF_Topics/sspData_JSON/SSP_Keyboard Maestro Discourse - Topics.json

I provide this Path setting up sspData like this:

Variable sspData as Local__sspDataFullPath

If I use this approach - I get this Error from the Spotlight Search Prompt Macro:

SSP Parse Error

The JSON itself is working but since it contains currently more than 2300 lines I decided that the Macro should use the JSON as file because it will grow in size.

Here are two Screenshots of the Prompt in action with the JSON as Text inside the Macro:

What I am doing wrong when getting the JSON to work from a File?

Please note that i am new to everything in parsing JSON correctly

Greatings from Germany

Tobias

Without seeing all of your code, I hesitate to suggest a solution. However…

In this step,

Variable sspData as Local__sspDataFullPath

you are putting the path to your JSON file into variable sspData. I suspect that what you want in sspData is the contents of that file, not the path to it. If that's the case, use the "Read a File" action.

1 Like

It's hard to tell without looking at the JSON itself. But since you're new to JSON, here's my guess:

When you set a JSON value, it needs to be properly "escaped". For example, consider this Dictionary:

image

Since the "Value" has question marks in it, it has to be encoded like this in JSON:

{
  "Key3": "Click the Help button (the \"question mark\") for more information."
}

This happens automatically for you if you use something like KM's Set JSON Value action. But if you built the JSON string by hand, you probably didn't encode it properly.

So my guess is that maybe there's an unescaped quote (or something similar) right before it barfs on the "/".

How good was my guess? :grinning_face_with_smiling_eyes:

Hello @drdrang ,

Your suggestion was absolutely right !! You nailed it !!
I was so confused about the Parse error and not nowing how to fix it that I totally forgot about the possibilities with the "Read a File" action. I even forgot that this Action exists ....

Sometimes it is the simple things you don't even think of.... thanks for that...

I even changed my Macro to use the "Read a File" action - and it works like a charm now.

Greetings from Germany

Tobias

2 Likes

Hey @DanThomas

I am building the JSON by hand using snippets - my knowledge is not at the level i need to build an entire JSON array using KM yet.
For Validation and viewing how many mistakes i've made I use a very handy Application named DevUtils.

I am sorry that your guess was not right - but you even helped me by the fact that I totally forgot about the possibility to use double quotes instead of just single ones in a JSON Value String when I escape them....

Thanks for that...

Greetings from Germany

Tobias

I have to laugh - as I scanned the sentence above, my mind read it as "I've made a very handy Application...", and when I looked at it, I thought "WTF? I thought he said he was just learning JSON!" A second look showed that it was an app you use, not one you made. :slight_smile:

It looks very cool. I would think it would have the ability to automatically "escape" a string for inclusion in a JSON string.

You've made me laugh, too Dan...

Your mind was really thinking that I am the Developer behind DevUtils ? - Very funny ...
I know something like that very well. Sometimes you're reading something and your brain has it's own message behind that.

Yeah. It's a very cool Application... and it has a lot of handy tools in it ...
I've not tested that but I think that you're right and it should escape Strings automatically.

But in such a case I try to do the opposite way and look for properly escaped strings during my writing of code because my brain is focused on the Thing I am writing and and scans for the right syntax color at the same time...

Greetings from Germany

Tobias

Yep! Assuming, of course, that my brain isn't off somewhere in the clouds.

I just posted this, in case you need it:

Thank you Dan for providing this .... I took a quick look at Your new Macro Post. Seems like a very handy tool to have - as always when you post macros.
I don't know how many Stuff i have only from You (maybe 45 ore more Macros - maybe all ?!) - but I will definetely take a closer look at this new stuff later the next few days when I have time.

Greetings from Germany

Tobias

Thanks for mentioning that - it makes me feel good. For me, I'd say the same thing about @ccstone's stuff, so it's good to know I'm paying it forward.

I will definetely take a closer look at this new stuff later the next few days when I have time.

Don't feel obligated. Personally, I hate it when I feel obligated to look at something someone posted. :grinning_face_with_smiling_eyes: I just figured there might be other people who might benefit from it.

1 Like

You're welcome, Dan...

No, of course not ... I am thinking the same way like you ... but here is the Thing - I think it is worthit for me to take a closer look so that I can figure out how I can use this in my json codin workflow.

Greetings from Germany

1 Like