How to filter and get specific keys from JSON objects array

I have a JSONB field containing a value in this structure:

[     {         "type": "small",         "name": "Josh",         "greeting": "Hello buddy!",         "hobby": "billiards",         "place": "Barcelona"     },     {         "type": "big",         "name": "Aziz",         "greeting": "Hey man!",         "hobby": "tennis",         "place": "Montpellier"     } ] 

I want to include this output if type is equal to "big":

{     "greeting": "Hey man! How're you doing Aziz?",     "place": "Montpellier" } 
  1. In the first row, I combined two values into one with some additional words.
  2. I don’t want my whole output to be empty if there isn’t type value equal to "big". Instead, output values of type value equal to "small" as a fallback.
  3. If there is not a fallback with type equal to "small", then output an empty string or NULL value but without Nulling the whole query result. Or preferably, not include these keys if no value is present.

I know I can combine values into one by using CONCAT but don’t know how to build this query and integrate it. I can’t loop through the array and output the values of the object that matches my filter. Tried CASE but still wasn’t able to get the result I want.

I use PostgreSQL 13.

Generic update of “normal” columns with data from JSON

I am trying to do a "generic" insert/update of table columns using JSON. There are tons of info how to update a JSON column, but I have found none that updates several normal columns in a table generic.

Insert columns with JSON data works

This works as I understand it in a generic way:

INSERT INTO inputtable SELECT * FROM json_populate_record (NULL::inputtable,     '{       "id": "0",       "name": "orkb type foo examples tutorials orkb",       "sum": 5743,       "float_col": 94.55681687716474     }' ); 

Fiddle: https://www.db-fiddle.com/f/eaQG8H4yqY9hnQBZjzJgz/29

Update columns with JSON data?

There are examples of "non-generic" update using JSON, but I am searching for a "generic" solution. In my dreams it should work as this pseudo code:

with data as ( select * from json_each_text(     '{       "id": "0",       "name": "orkb type foo examples tutorials orkb",       "sum": 5743,       "float_col": 95     }') ) update inputtable set = (select * from data) where id=0 

Is this possible to do? If it is, how do I do this?

customise JSON object of a block

A simple wp block:

<!-- wp:video {"id":155742} --> <figure class="wp-block-video"><video controls src="http:vidoe.mp4"></video></figure> <!-- /wp:video --> 

How to achieve something along those lines:

<!-- wp:video {"id":155742, "poster_id":155654 } --> <figure class="wp-block-video"><video controls src="http:vidoe.mp4"></video></figure> <!-- /wp:video --> 

I’ve been going through many articles on adding custom attributes to blocks but can’t find reference to modify the JSON object that is included in the HTML comment that WP adds to blocks. Is there a hook to modify the JSON object?

WordPress JSON return unknown characters fo non English characters

for a project i create an endpoint something like wp-json/HSE/v1/reports which return json file everything is okey , also Engligh words , but for non English words i have real problem that its bring back something like \u0645\u0627\u0647\u0627\u0646 \u0633\u06cc\u0631\u062c\u0627\u0646 its confusing me at all .

i also check the wp-json/wp/v2/posts and watch the same problem .English words are fine but non English words are not readable .

what should i do to fix this ? anyone can help me please ?

Parse Data on runtime into JSON

I have been trying to work out a subscription form for my game and thanks to you guys here, I came to a solution which works but still needs some changes.

The idea is to have an input field in the scene that takes the users email and convert it into JSON (to do a POST request). I have made the post request and everything works fine but I have to manually type the email in the inspector. What I would want is to get that value from the input field!

    public class testing : MonoBehaviour     {         public TMP_InputField myField;         //public InputField field;         [SerializeField]         private Email _email = new Email();         private string URL = "";          public void SaveData()         {             string data = JsonUtility.ToJson(_email); // this part here needs to be like _email.text             System.IO.File.WriteAllText(Application.persistentDataPath + "Data.json", data);             StartCoroutine(SaveIntoJson(URL , data));         }         IEnumerator SaveIntoJson(string url, string data)         {             var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);             request.SetRequestHeader("Content-Type", "application/json");             var jsonBytes = Encoding.UTF8.GetBytes(data);             request.uploadHandler = new UploadHandlerRaw(jsonBytes);             request.downloadHandler = new DownloadHandlerBuffer();                  yield return request.SendWebRequest();             if (request.isNetworkError || request.isHttpError)             {                 Debug.Log(request.error);                 Debug.Log(request.downloadHandler.text);             }             else             {                 Debug.Log("Form upload complete!");             }             Debug.Log(data);         }     }     [System.Serializable]     public class Email     {         public List<Profiles> profiles = new List<Profiles>();     }     [System.Serializable]     public class Profiles     {         [SerializeField]         public string email; // this part here works but i need to type the email in the inspector     } 

also the requirements for the json are like this

 {"profiles":[{"email":"testing@tes.com"}]} 

How can I separate it with Json decode and save it in PostgreSQL database with PHP?

I want to do web scraping with PHP. There is a json data in the URL, I want to pull this data and save it to the postgreSQL database. This is the code:

<?php  $  ch = curl_init(); $  url = "https://kurumsal.sokmarket.com.tr/ajax/servis/sehirler";  curl_setopt($  ch,CURLOPT_URL, $  url); curl_setopt($  ch,CURLOPT_RETURNTRANSFER, true);  $  resp = curl_exec($  ch);  if($  e = curl_error($  ch)) {     echo $  e; } else{     $  decoded = json_decode($  resp, true);     print_r($  decoded); }    //your database connection here  $  host = "localhost";  $  user = "postgres";  $  password = "****";  $  dbname = "sok";  // Create connection try{     $  this->linkid = @pg_connect("host=$  this->host port=5432 dbname=$  this->dbname user=$  this->user password=$  this->password");     if (! $  this->linkid)     throw new Exception("Could not connect to PostgreSQL server."); } catch (Exception $  e) {     die($  e->getMessage()); } foreach ($  array_data as $  row) { $  sql = "INSERT INTO il_adi (il) VALUES (decoded)"; $  conn->query($  sql); } $  conn->close();   ?> 

I can view the data I have captured in the array in the terminal. How can I save this to the database?

JSON Creation Issue

I’m new to PostgreSQL database. Although I’ve good experience on Oracle Database. I’m facing issue while generating JSON from Postgres. I’ve tried to generate the same output but not able to achieve is properly. Can someone please help or guide may be with examples on similar problem. I’ll be very much thankful to you.

"PostgreSQL 12.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit"

Tables and Sample Data.

CREATE TABLE PARKING_LOT (       PARKING_LOT_ID      NUMERIC(10)      ,PARKING_LOT_NAME    VARCHAR(100) ) ;  CREATE TABLE PARKING_LOT_VEHICLE_CLASS_MAP (       PARKING_LOT_ID       NUMERIC(10)      ,VEHICLE_CLASS_ID     NUMERIC(10)      ,AVAILABLE_SLOT_COUNT NUMERIC(10) ) ;   CREATE TABLE PARKING_SLOT (       PARKING_LOT_ID          NUMERIC(10)      ,VEHICLE_CLASS_ID        NUMERIC(10)      ,PARKING_SLOT_ID         NUMERIC(10)      ,SLOT_OCCUPANCY_STATUS   NUMERIC(10) ) ;   INSERT INTO PARKING_LOT VALUES( 1, 'PARKING 1' ) ; INSERT INTO PARKING_LOT VALUES( 2, 'PARKING 2' ) ;  INSERT INTO PARKING_LOT_VEHICLE_CLASS_MAP VALUES( 1, 1, 10 ) ; INSERT INTO PARKING_LOT_VEHICLE_CLASS_MAP VALUES( 1, 1, 11 ) ; INSERT INTO PARKING_LOT_VEHICLE_CLASS_MAP VALUES( 2, 2, 12 ) ; INSERT INTO PARKING_LOT_VEHICLE_CLASS_MAP VALUES( 2, 2, 13 ) ;  INSERT INTO PARKING_SLOT VALUES( 2, 1, 1, 1 ) ; INSERT INTO PARKING_SLOT VALUES( 2, 1, 2, 1 ) ; INSERT INTO PARKING_SLOT VALUES( 2, 2, 3, 0 ) ; 

Desired Output

{    "ResHeader":{       "ResDate":"09-01-2021 12:38:20",       "ResID":"12345",       "ResName":"Occupancy",       "ResDesc":"Parking Lot Availability and Occupancy Status"    },    "ResDetail":[       {          "ParkingLotID":"1",          "ParkingLotName":"PARKING 1",          "ParkingLotOccupancySummary":[             {                "VehicleClassID":"1",                "AvailableSlotCount":"10"             },             {                "VehicleClassID":"2",                "AvailableSlotCount":"12"             }          ]       },       {          "ParkingLotID":"2",          "ParkingLotName":"PARKING 2",          "ParkingLotOccupancySummary":[             {                "VehicleClassID":"1",                "AvailableSlotCount":"5",                "SlotOccupancyDetails":[                   {                      "SlotID":"1",                      "OccupancyStatus":"1"                   },                   {                      "SlotID":"2",                      "OccupancyStatus":"1"                   }                ]             },             {                "VehicleClassID":"2",                "AvailableSlotCount":"7",                "SlotOccupancyDetails":[                   {                      "SlotID":"3",                      "OccupancyStatus":"0"                   }                ]             }          ]       }    ] } 

What I’m able to achieve till now in bits and pieces

WITH     HEAD AS     (         SELECT json_build_object( 'ResHeader', H ) HeaderData         FROM         (             SELECT                  TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.MS') "ResDate"                 ,1  "ResID"         ) H     ) SELECT * FROM HEAD H  SELECT X.PARKING_LOT_ID, row_to_json(X) FROM (     SELECT VCM.PARKING_LOT_ID, VCM.VEHICLE_CLASS_ID  "VehicleClassID"     FROM PARKING_LOT_VEHICLE_CLASS_MAP2 VCM     LEFT OUTER JOIN PARKING_SLOT2 PS     ON VCM.PARKING_LOT_ID = PS.PARKING_LOT_ID ) X 

Regards Manoj

Generate JSON files for language translation from po file without wp-cli i18n make-json

My plugin uses wp_set_script_translations() to load translations for JS as mentioned here https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/.

I understand that when translations for a locale is added translate.wordpress.org it will create the JSON files.

But, we have a lot of site owners wants to translate a few words and start using the plugin. They can create .po and .mo files, but how they can create the JSON files without setting up wp-cli to use (wp i18n make-json).

Loco Translate plugin is not supporting JSON file generation.

I couldn’t find any online sites supporting .po to .json(with below mentioned format) conversion.

Out put file should in $ {domain}-$ {locale}-$ {handle}.json or $ {domain}-$ {locale}-$ {md5}.json JED 1.x ( .json ) file format for each JS file that called with wp_set_script_translations().

Alternative approaches to Iframes for content distribution via json api

I am currently working on a project that uses iframes to distribute content to customers. Going ahead we would like to switch to a json based rest api to deliver the content. Api access would need a token to which specific content could be exposed and traffic limits set.

To replace the frontend appearance of the iframe I am thinking about writing a reusable bundle using a lightweight react alternative like preactjs. But this would mean exposing the raw api and the specific token to the end user. Simply routing user requests via the customers server would conceal the token but still allow raw api access to the enduser.

What would be a good architecture for such a use case?

Are there server side rendered solutions that can easily be implemented across a variety of backend frameworks, without rewriting everything for each customer that is?

Thanks for any advice

Deserialize only some fields from a JSON file

I’m trying to parse a very large JSON string in Unity. I do not need all the fields and I can’t create a class with all the members.

It appears that all examples I have found, including the official docs, always deserialize and map every field to class members, which I cannot do for my application.

I tried creating a class that is similar to the JSON string, meaning that it has some members of the target JSON, just not all of them. But it’s not working as desired: all members of my class are always undefined after deserializing.

Here’s part of the string:

{     "Robots":[{"CanReset":false,     "CycleTime":123.875,     "Info":null,     "LevelInfo":null,     "Name":"FTF_10033",     "State":0,"     ...     }, ... } 

So I tried creating these classes:

[Serializable] public class jsonData {     public Robot[] robots; }  [Serializable] public class Robot {     public string Name = "Unknown AGV"; } 

And printing the results:

var jsonString = www.downloadHandler.text; jsonData jsonData_ = JsonUtility.FromJson<jsonData>(jsonString); Debug.Log("Json data: " + jsonData_); Debug.Log("Robots: " + jsonData_.robots); 

jsonData_ prints Json data: jsonData_, and jsonData_.robots prints nothing (just Robots:).

Is there any way to parse just these particular fields out of the JSON string?

I would be content with something like jsonString["robots"][0]["name"] or something.