How to deserialize real world JSON in Symfony2 - php

I have a directory into which some json files are regularly updated. What I want to do is deserialize them in my Symfony2 application to get at their juicy data.
The examples on the symfony site include extremely simple flat JSON examples, that do not reflect the nested reality of real world nested JSON data. For example, the following is a simplified version of the files I want to deserialize.
{
"uid" : "some unique identifier"
"title" : "this is a tile",
"description" : "some description",
"paragraphs" : [
{
"position" : "left",
"body" : "a lot of text here",
"video":{
"ogg" : "path1",
"webm" : "path2",
"mp4" : "path3"
}
},
{
"position" : "right",
"body" : "a lot of text here",
"video":{
"ogg" : "path1",
"webm" : "path2",
"mp4" : "path3"
}
}
]
}
Of course, I want to deserialize this nested JSON into a simple, easy to access, model.
What I want to know is how to write the Content class for the above JSON so that when I call $filecontent = $serializer->deserialize($data, 'Acme\Content', 'json'); it deserializes successfully.

This should deserialize your JSON with ease:
$fileContent = json_decode($jsonData);
Documentation for json_decode can be found here

Related

PHP - Parsing JSON Payload Issue

I'm receiving some JSON via a HTTP POST Callback to a PHP page and I'm having an issue parsing out the JSON. Here's an example of what the JSON data that is sent looks like:
[
{
"type" : "message-received",
"time" : "2016-09-14T18:20:16Z",
"description" : "Incoming message received",
"to" : "+12345678902",
"message" : {
"id" : "14762070468292kw2fuqty55yp2b2",
"time" : "2016-09-14T18:20:16Z",
"to" : ["+12345678902"],
"from" : "+12345678901",
"text" : "Hey, check this out!",
"applicationId" : "93de2206-9669-4e07-948d-329f4b722ee2",
"media" : [
"https://messaging.bandwidth.com/api/v2/users/{accountId}/media/14762070468292kw2fuqty55yp2b2/0/bw.png"
],
"owner" : "+12345678902",
"direction" : "in",
"segmentCount" : 1
}
}
]
I'm then processing this as follows:
$eventJSON = file_get_contents('php://input');
$event= json_decode( $eventJSON );
$eventType = $event->type;
but I'm not getting anything so far for my $eventType variable - I think the issue might be that the JSON is an array but I'm not sure how to handle this?
To parse json try
$eventType = $event[0]->type;
Refer :- How do I extract data from JSON with PHP? to know the difference between object properties and array elements

Extracting Caption from Image using Facebook PHP SDK Transformer

I have problems extracting attribute text from Image tag using the Facebook Instant Articles SDK Transformer
I cannot figure out the rules.json required to extract the text from alt attribute and make a caption out of it.
//MARKUP
<img src="https://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg" alt="Foto By: Bla Bla"/>
//RULES.JSON
{
"class": "ImageRule",
"selector" : "img",
"properties" :
{
"image.url" :
{
"type" : "string",
"selector" : "img",
"attribute": "src"
},
"image.caption" :
{
"type" : "string",
"selector" : "img",
"attribute" : "alt"
}
}
}
Expected results are Facebook Instant Article compliant markup like:
<figure>
<img src="https://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg"/>
<figcaption>Foto By: Bla Bla</figcaption>
</figure>
What I get is Uncaught Error: Call to a member function hasChildNodes() on string in /Facebook/InstantArticles/Transformer/Transformer.php on line 305.
Somehow image gets processed, the caption gets processed, I get the correct value but then it recursively again enters transform function passing in the extracted "alt" string and it fails because it expects an HTML Node input not a String.
Facebooks documentation on the matter is extremely vague so if anyone has some experience dealing with Facebook Instant Articles please chime in.
shitty docs can be found here:https://developers.facebook.com/docs/instant-articles/sdk/transformer/
https://developers.facebook.com/docs/instant-articles/sdk/transformer-rules
main committer of SDK here.
You can check the setup we have into the SimpleTransformerTest.php that covers exactly your need. You can also use any tests to play around with the Transformer.
What you are doing wrong is the selector for the image.caption that should be a type of element.
For your Rules.json it should look like:
{
"class": "CaptionRule",
"selector" : "//img[#alt]",
"properties" : {
"caption.default": {
"type": "string",
"selector": "img",
"attribute": "alt"
}
}
},
{
"class": "ImageRule",
"selector" : "figure",
"properties" :
{
"image.url" :
{
"type" : "string",
"selector" : "img",
"attribute": "src"
},
"image.caption" :
{
"type" : "element",
"selector" : "img"
}
}
}
Check that I'm using a different strategy instead of going straight to the <img> element on the ImageRule, I'm picking the <figure> tag, so then we can keep the transformer intact.
Note that the rules.json are applied bottom up.
Let me know if this covers your need.

how to append information to a document in mongo?

Background Information
I have the following data in my mongo database:
{ "_id" :
ObjectId("581c97b573df465d63af53ae"),
"ph" : "+17771111234",
"fax" : false,
"city" : "abd",
"department" : "",
"description" : "a test"
}
I am now writing a script that will loop through a CSV file that contains data that I need to append to the document. For example, the data might look like this:
+17771111234, 10:15, 12:15, test#yahoo.com
+17771111234, 1:00, 9:00, anothertest#yahoo.com
Ultimately I want to end up with a mongo document that looks like this:
{ "_id" :
ObjectId("581c97b573df465d63af53ae"),
"ph" : "+17771111234",
"fax" : false,
"city" : "abd",
"department" : "",
"description" : "a test",
"contact_locations": [
{
"stime": "10:15",
"etime": "12:15",
"email": "test#yahoo.com"
},
{
"stime": "1:00",
"etime": "9:00",
"email": "anothertest#yahoo.com"
},
]
}
Problem
The code I've written is actually creating new documents instead of appending to the existing ones. And actually, it's not even creating a new document per row in the CSV file... which I haven't debugged enough yet to really understand why.
Code
For each row in the csv file, I'm running the following logic
while(!$csv->eof() && ($row = $csv->fgetcsv()) && $row[0] !== null) {
//code that massages the $row into the way I need it to look.
$data_to_submit = array('contact_locations' => $row);
echo "proving that the record already exists...: <BR>";
$cursor = $contact_collection->find(array('phnum'=>$row[0]));
var_dump(iterator_to_array($cursor));
echo "now attempting to update it....<BR>";
// $cursor = $contact_collection->update(array('phnum'=>$row[0]), $data_to_submit, array('upsert'=>true));
$cursor = $contact_collection->insert(array('phnum'=>$row[0]), $data_to_submit);
echo "AFTER UPDATE <BR><BR>";
$cursor = $contact_collection->find(array('phnum'=>$row[0]));
var_dump(iterator_to_array($cursor));
}
}
Questions
Is there a way to "append" to documents? Or do I need to grab the existing document, save as an array, merge my contact locations array with the main document and then resave?
how can I query to see if the "contact_locations" object already exists inside a document?
Hi yes you can do it !
1st you need to find your document and push the new value you need :
use findAndModify and $addToSet :
$cursor = $contact_collection->findAndModify(
array("ph" => "+17771111234"),
array('$addToSet' =>
array(
"contact_locations" => array(
"stime"=> "10:15",
"etime"=> "12:15",
"email"=> "test#yahoo.com"
)
)
)
);
The best part is $addToSet wont add 2 time the same stuff so you will not have twice the same value :)
Here the docs https://docs.mongodb.com/manual/reference/operator/update/addToSet/
I'm not sure the exact syntax in PHP as I've never done it before but I'm currently doing the same thing in JS with MongoDB and $push is the method you're looking for. Also if I may be a bit nitpicky I recommend changing $contact_collection to $contact_locations as a variable name. Array variable names are usually plural and being more descriptive is always better. Also make sure you find the array in the MongoDB first that you want to append to and that you use the MongoDb "update" command

PHP JSON object nested loop data syntax

I'm trying to loop through some JSON data and pull out specific values. Here is the JSON data and the partially working code.
$jsondata = '
[
{
"id" : "421356",
"trip_update" : {
"trip" : {
"trip_id" : "421356",
"start_time" : "12:05:00",
"start_date" : "20130926",
"route_id" : "15"
},
"stop_time_update" : {
"stop_sequence" :70,
"departure" : {
"delay" : 240,
"time" : 1380215057
},
"stop_id" : "6090"
},
"stop_time_update" : {
"stop_sequence" :71,
"departure" : {
"delay" : 240,
"time" : 1380215075
},
"stop_id" : "6095"
}
}
}]';
$result = json_decode($jsondata);
foreach($result as $value) {
echo "trip_id: ".$value->trip_update->trip->trip_id;
if (gettype($value->trip_update ) == "object") {
foreach($value->trip_update as $item) {
echo " - stop_sequence: ".$item->stop_sequence;
}
}
}
I can get the first level of data under 'trip_update->trip'. But there can be any number of 'stop_time_update' data within 'trip_update' as well. Since this data relates to the trip_update data, I need to loop through it and correlate it.
The end goal is to save this data to a database (not shown in the code), so for clarity, this would be the simplified 2 rows of DB data I would like to save in this example:
trip_id,stop_sequence
421356,70
421356,71
There can be any number of stop_sequences in the source data.
Here is an interactive link to the code for you to edit or mess with:
http://sandbox.onlinephpfunctions.com/code/f21ca8928da7de3e9fb351edb075d0a446906937
You might get better results if you write your own parser or use a stream-parser with callbacks. Here's a PHP implementation of such a parser that works with callbacks. So instead of reading the whole JSON data into memory, the parser will read the data in chunks and notify your "listener-class" whenever a new object starts or a property was read in etc. By doing this, you should get separate callback events for each stop_time_update property instead of just one value in the parsed array.
Very similar to what SAX is for XML.
Hi maybe you can change the name.
function next_update($coincidencias){
$replace=$coincidencias[0].$GLOBALS["json_stop_time_update"]++;
return $replace;
}
$result= preg_replace_callback("/stop_time_update/","next_update",$jsondata);
$result = json_decode($result);
You should rework your JSON - you have multiple keys with the same name, try to do print_r($result) to see what I am talking about - PHP will override the "stop_time_update" key time after time and you will be able to access only the last entry. Instead, you should organize your JSON like that:
[
{
"id" : "421356",
"trip_update" : {
"trip" : {
"trip_id" : "421356",
"start_time" : "12:05:00",
"start_date" : "20130926",
"route_id" : "15"
},
"stop_time_update" : [{
"stop_sequence" :70,
"departure" : {
"delay" : 240,
"time" : 1380215057
},
"stop_id" : "6090"
}, {
"stop_sequence" :71,
"departure" : {
"delay" : 240,
"time" : 1380215075
},
"stop_id" : "6095"
}]
}
}]
then you will be able to iterate through your data like this:
foreach($result[0]->trip_update->stop_time_update as $update)
{
$time = $update->departure->time;
...
}
If you cannot change the data structure, then what probably could help you is a PULL parser - one that does not return parsed data structure, but allows you to use a data stream instead - this way you could iterate over each node. The only one I managed to find is an extension to PHP:
https://github.com/shevron/ext-jsonreader
Check the usage section.
This JSON response is invalid because it contains duplicate keys but JSON doesn't allow duplicate keys.
You should contact the service you're trying to request this response from.
If you have a valid JSON response then you can decode it using the json_decode function which returns an object or an array (depends on the second parameter);
You cannot use a JSON parser for this as it will always overwrite the first element due to the same keys. The only proper solution would be asking whoever creates that "JSON" to fix his code to either use an array or an object with unique keys.
Another option is to write your own decoder function for parse it

Export from MySQL with relations to JSON (for CouchDB) with PHP

I’m working on my master thesis where one of my goals is to run tests and experiments against the CouchDB database and tune the performance.
To do that I need some test data. I’ve created a piece of php code to generate some simple relational data for a MySQL database. The tables are:
Customer
Product
Brand
Color
Checkout
I’ve made some relations between for example Product and Colorid and Brandid and in the Checkout table I’ve a relation to Customerid and Productid.
I want to export this entirely data structure and the data with its relations to a JSON format for CouchDB.
So I’ve a JSON string which should contain each customer with its attributes and its purchase with all the parameters for this product, and so on.
I’m thinking that it would look something like:
{
"customer": {
"customerid" : "1",
"firstname" : "somefirstname",
"lastname" : "somelastname",
"email" : "my#mail.com",
"country" : "USA",
"datecreated" : "11111111111111"
}
"purchase" : {
"purchaseid" : "1",
"product": {
"productname" : "mightymouse",
"productcolor": "blue",
"productbrand" : "Apple",
"productprice" : "200",
"checkoutdate" : "1111111111112"
}
"purchaseid" : "2",
"product": {
"productname" : "something nice",
"productcolor": "yellow",
"productbrand" : "Google",
"productprice" : "5000",
"checkoutdate" : "11111111113333"
}
}
}
It’s probably not the right data structure I’ve shown but it something like that.
Can this be done in PHP and if so, how do I create this kind of “CouchDB” ready JSON strings??
If I haven’t explained myself clearly, please let me know.
Thank you
Sincere
- Mestika
PHP has function json_encode, you can use it to convert any PHP object to its JSON representation (and json_decode to convert JSON string to object).
So:
Use PHP database functions to read data from database.
Create object of stdClass:
$data = new stdClass;
Fill this object with properties read from database like this:
$customer = new stdClass;
$customer->customerid = "1"
...
$data->customer = $customer;
Encode generated object with json_encode.

Categories