Need help: POST to WordPress using nested JSON array in Shortcuts

Hi,

I’m trying to post to my micropub-enabled Wordpress blog using the “Get Contents of URL” action in Shortcuts, and I’m running into problems.
I’m not really techy, I just cut and paste my way through a lot of my automation, so I’m hoping someone can help me.

This is an example of the JSON I’m trying to send:

{
  "type": ["h-entry"],
  "properties": {
    "summary": ["Want to read: Title by Author, ISBN: ISBN"],
    "read-status": ["to-read"],
    "read-of": [
      {
        "type": ["h-cite"],
        "properties": {
          "name": ["Title"],
          "author": ["Author"],
          "uid": ["isbn:ISBN"]
        }
      }
    ]
  }
}

It’s a nested JSON array and I don’t know how to do that in Shortcuts.

The screenshot doesn’t match the code above exactly - I was testing something when I took the screenshot - but it illustrates what I was trying to do. I’d attempted to:

  • nest a dictionary within the Request Body section and it gets ignored
  • nest a text variable as text, but it would wrap the entire text block in quotations
  • select “JSON” in the Request Body section and point it at a text variable with the entire JSON array
    • this worked, however, I also want to post an image which I could do if I use the “FORM” method, but I don’t know how to embed an image in a JSON array that’s a text variable.

Help please?

I’m guessing this is not the kind of thing people do?

What is the actual question you want to answer? The original post doesn’t seem to say exactly and as you note, your screenshot does not match with what you describe.

You also have not shares your shortcut as a starting point. Without that, it is hard to know what exactly you have done. Also it may be that your question relies on a particular scenario that isn’t in the sphere of experience for most people, but where they may be able to help with an underlying logic problem.

Is it JSON you are having issues with? Shortcuts working with JSON? Your WordPress API call? Encoding an image into JSON data?

If it happens to be all of the above or more, my advice would be to pick one you can fix and test a fix for. You are far more likely to get help with one detailed and specific question than a generalised request or numerous non-sequential questions.

Let me try to reiterate.

I am trying to POST the above JSON array using Shortcuts, but I am running into trouble with the nested portion of the array. Shortcuts keep wrapping my entire nested portion in quotes and I’m trying to get around that.
In my original post I specified the steps I’ve taken and how that had failed.

So my question is, how would you fill in the “Request Body” of the “Get Contents of” action so that it would reflect the JSON array that I posted above?

As for sharing my shortcut, since it has my API key, it’s not something I could share.

I understand that this is possibly outside the sphere of experience for most people. This shortcut is supposed to be useful for the small group of people who uses WordPress and Micro.blog, who also wants to post their Read status to their website instead of GoodReads. It’s quite a small audience.

Since you have the JSON expressed in raw text already, try placing that in a text action, then following this with a Get dictionary from input action, and then referencing that resulting dictionary in your POST body using a magic variable.

You could dummy in an API key. Sanitising a shortcut is a standard approach.

I only noted this as you said “I’m guessing this is not the kind of thing people do?”

So you got it working with the JSON and the only question is how you can send an image with the request?

I don’t know the structure of the JSON where the image would go, but to get a text representation you can encode it with base64. There is an action for that in shortcuts. You then just have to figure out (or read in the documentation) how to include the image. If this isn’t possible, you will probably need to send 2 requests.

If it works without converting the text to a dictionary then don’t convert it. The reason is that arrays with just a single element will get “unpacked” and the resulting JSON is not the same as the initial one. For example: "type": ["h-entry"] will be converted to "type": "h-entry". This may be no problem if the API can deal with it, but most of the time APIs follow a specific structure and fail if there’s just a single thing wrong.