How best to unwrap a stringified JSON

Hey all,

I’ve used a custom stringify formatter to pass a full JSON output from the client side to server side, to then store in a single text field in the DB, which is great.

Ideally I’d now like to do more with this, but can’t seem to convert any parts of the now stringified JSON back into an array/object for use in other server connect actions.

Any ideas?

1 Like

Have you tried

var myobj = JSON.parse($string);

Sorry that may be a dumb question.

Hey @spearstone - not a dumb question at all.

I haven’t, as I’m purposefully using stringify from client > server side so I can input the full JSON into a db field.

What I’m now trying to do is work with the stringified data in Server Connect, and somehow convert sections of back arrays I need to use.

On the server action end, you might have to take help of a custom formatter which would convert the JSON String to JSON.
I am not aware of any built-in option to convert JSON string to JSON.

Or, you can make use of your SQL server to return the string JSON data in columns using a custom query or SP. I have read about this, but have never done it myself.

There are although I think not in the UI.

    toJSON: function(val) {
        return JSON.stringify(val);
    },

    parseJSON: function(val) {
        return JSON.parse(val);
    }

Hey @JonL - I missed your reply here!

Do you think this would be possible to use and convert to a custom formatter on SC, or only as a script on the client side on the page?

Something like this:

js file:

exports.parseJSON = function(val) {
    return JSON.parse(val);
};

hjson file:

{
  type: 'method_parseJSON',
  groupTitle : 'Custom Formatters',
  groupIcon : 'fa fa-lg fa-key',
  addTitle: 'parseJSON',
  title : 'parseJSON',
  icon : 'fa fa-lg fa-hashtag',
  state : 'opened'
  properties : [
{
  group: 'JSON String Input',
  variables: [
    {
      name: 'jsonStringInput', optinName: 'jsonStringInput', title: 'jsonStringInput', type: 'text'
    }
  ]
}
  ]
}
1 Like

You could but it seems a lot of effort for a simple formatter thst doesn’t take additional arguments or has a complex config. You just need to chain .parseJSON() to your stringified value.

2 Likes

Thanks @JonL and understood, I’ve just not been able to unravel stringified JSON whatever I try, so thought this might be a good way to handle it.

Just returning to this, and this worked well in the end - don’t know why I couldn’t get it working previously, so thank you!

single input form data with full json array > sc > add your chain and it unwraps the json perfectly, thank you.

Now to figure out how to add the various nested arrays into the DB correctly!

1 Like

we cannot still parse json on client side - it would be nice to have built into wappler.