Jump to content

TextformatterJsonDecode


nbcommunication
 Share

Recommended Posts

The simplest, quickest, and maybe most useful module I've ever built ?

TextformatterJsonDecode: Passes the given text through json_decode(), returning the value (if valid) as a stdClass object.

Didn't even bother with a README - does what it says on the tin. Ha need one for the modules repo...

This is particularly useful if you are storing JSON from an API in a Page field and want to access it in a similar way to normal Page objects e.g.

<?php

/* The data
{
	"fieldName": "value"
}
*/

echo $page->api_data->fieldName; // value

I'll be using this extensively over the next few weeks, looking forward to it ?

  • Like 6
  • Thanks 2
Link to comment
Share on other sites

  • 2 weeks later...

Nice! I love it. Today, while working with JSON stored in Page fields, I received the newsletter containing a link to your module. I instantly felt that it was perfect for replacing my json_decode calls.

Just out of curiosity, what do you typically do when you need to retrieve a value, modify it and save the value again in the field? I was doing:

$page->of(false);
$json = json_decode($page->jsonfield, false);
$merge = array_merge($json, $otherJson); //$otherJson is a json I want to push to the field
$json = json_encode($merge);
$page>save();

I'm trying to understand how to  get the field with the textFormatter in that situation, because if I call page->of(false) before retrieving the value, obviously the value came as a string, but If I set page->of(false) after retrieving the value, the value is not saved. 

$json = $page->jsonfield;
$page->of(false);
$merge = array_merge($json, $otherJson); //$otherJson is a json I want to push to the field
$json = json_encode($merge);
$page>save(); //Isn't saving

 

Link to comment
Share on other sites

Hi @Sanyaissues,

It might be that this Textformatter isn't the best fit for your use case, but to hopefully solve your issue:

json_decode() can return a stdClass object or an associative array. This module returns a stdClass object. I don't think you can use array_merge() on this (might be wrong, never tried it).

To retrieve the field, modify it and save it, this should work:

<?php

$otherArray = [];

$page->of(false);
$array = json_decode($page->jsonfield, 1); // associative array
$page->jsonfield = json_encode(array_merge($array, $otherArray));
$page>save();

// Or

$array = json_decode($page->getUnformatted('jsonfield'), 1); // associative array
$page->setAndSave('jsonfield', json_encode(array_merge($array, $otherArray)));

Cheers,

Chris

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...