Ok, I'm hoping this is not the same as other questions and I have looked for similar but can't find the answer... so here goes.
I get a JSON file and need to read the contents into a database. No sweat.
However I am having issue with reading the lowest levels as there can be a duplicate array.
I have all the data down to "comp".
Sample JSON file:
{
"code" : 0,
"format" : 2,
"s_data" :
{
"name" : "Lance",
"club" : "Southern"
},
"d_data" :
{
"errorcode" : 3,
"errormsg" : ""
},
"timing" :
{
"elapsed_time" : "08:29",
"elapsed_time_secs" : 509,
"comp" :
[
{
"no" :
[
{
"sno" : 0,
"xpos" : 230.9,
"ypos" : 97.2
},
{
"sno" : 1,
"xpos" : 132.4,
"ypos" : 258.3
},
{
"sno" : 2,
"xpos" : 135.5,
"ypos" : 176.7
},
]
},
{
"no" :
[
{
"sno" : 0,
"xpos" : 250.9,
"ypos" : 92.2
},
{
"sno" : 1,
"xpos" : 182.4,
"ypos" : 158.3
},
{
"sno" : 2,
"xpos" : 145.5,
"ypos" : 192.7
}
]
}
]
}
}
So I want to now read the "no" data, but just the first instance of "no"... that is: sno, xpos, ypos foreach "sno" entry..
I'm thinking:
$mydata = json_decode(sample.json, true);
foreach($mydata->comp[0]->no as $values)
{
echo $values->sno . "\n";
}
I get nothing back.
Any kind assistance appreciated.
If you decode it in an associative array, you have to use it according, try this code:
foreach($mydata['timing']['comp'][0]['no'] as $values)
{
echo $values['sno'] . "\n";
}
where all accesses to $mydata are using array style notation.
You have also to consider that the one posted is not a valid JSON string.
EDIT 20160122:
JSON string is now valid.
Related
I need to pass on a variety of variables from mysql queries to create a json file and post every block whenever $sku changes. I can retrieve the data just fine, but when I try to use json_decode, it either says syntax error or returns null, or worse, treats everything as text.
$updatedData = '{
"items" : [
{
"barcode" : $barkod,
"title" : $title,
"productMainId" : $sku,
"brandId" : $marka,
"categoryId" : 387,
"stockCode" : $sku,
"dimensionalWeight" : 0,
"description" : $desc,
"vatRate" : 0,
"images" : [
{
"url" : $image0link
}
],
"attributes" : [
{
"attributeId" : 343,
"attributeValueId" : $gender
},
{
"attributeId" : $mekanizma1,
"attributeValueId" : $mekanizma2
},
{
"attributeId" : $kasasekli1,
"attributeValueId" : $kasasekli2
},
{
"attributeId" : $camcinsi1,
"attributeValueId" : $camcinsi2
},
{
"attributeId" : $camsekli1,
"attributeValueId" : $camsekli2
},
{
"attributeId" : $kordonmlz1,
"attributeValueId" : $kordonmlz2
},
{
"attributeId" : $kordonrengi1,
"attributeValueId" : $kordonrengi2
},
{
"attributeId" : $kasacap1,
"attributeValueId" : $kasacap2
},
{
"attributeId" : $kasarengi1,
"attributeValueId" : $kasarengi2
},
{
"attributeId" : $sugecirmezlik1,
"attributeValueId" : $sugecirmezlik2
},
{
"attributeId" : $kasamlz1,
"attributeValueId" : $kasamlz2
},
{
"attributeId" : 47,
"customAttributeValue" : $kadranrengi
}
],
"cargoCompanyId" : 1
"shipmentAddressId" : 409558
"returningAddressId" : 409558
}
]
}';
{$json = json_decode($updatedData, true);
if (is_null($json)) {
die("Json decoding failed with error: ". json_last_error());
}
var_dump($updatedData);}
I'd appreciate any help. Thanks in advance.
I am having some issues parsing a json file from Jenkins using PHP
{
"actions" : [
{
"causes" : [
{
"shortDescription" : "Started by an SCM change"
}
]
},
{
},
{
},
{
"buildsByBranchName" : {
"origin/release_5.6.0" : {
"buildNumber" : 242,
"buildResult" : null,
"marked" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
},
"revision" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
}
},
"origin/release_5.7.0" : {
"buildNumber" : 315,
"buildResult" : null,
"marked" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
},
"revision" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
}
},
I have tried doing the following
//Read in JSON object
$json_file2 = file_get_contents('url.com/json');
//Decode JSON file
$test = json_decode($json_file2); //object
//print_r($json_file2);
echo $test->causes;
I am also trying to access the different sections in "buildsByBranchName". I have tried many different variations of the code above, but I keep getting "Undefined property: stdClass" errors.
You are not accessing that value properly. causes resides under actions which is an array. Your code also won't work because causes is an array.
// This is an array so you can't use echo here.
$causes = $test->actions[0]->causes;
// echo out the shortDescription
echo $causes[0]->shortDescription;
or
echo $test->actions[0]->causes[0]->shortDescription;
I have these documents in a mongoDB:
/* 1 */
{
"_id" : ObjectId("553ce99a39108e2b7c1edeb9"),
"coleccion" : "aplicaciones",
"nombre" : "Mascotas",
"descripcion" : "Censo de mascotas",
"tipo" : "privada"
}
/* 2 */
{
"_id" : ObjectId("553e316e39108e802a1edeb9"),
"coleccion" : "aplicaciones",
"nombre" : "otra aplicacionn",
"descripcion" : "w aplicacion",
"tipo" : "privada",
"campoId" : [
{
"id" : "1430145364",
"id_campo" : "553bffca39108eb163cff7aa",
"orden" : 90
},
{
"id" : "1430145368",
"id_campo" : "553bffed39108e346ccff7ab",
"orden" : 100,
"estado" : "0"
},
{
"id" : "1430145370",
"id_campo" : "553c001139108ebc63cff7aa",
"orden" : 29,
"estado" : "1"
},
{
"id" : "1430145395",
"id_campo" : "553c001139108ebc63cff7aa",
"orden" : 9,
"estado" : "0"
}
]
}
I need to query and sort the data in ascending order of each document using the field " campoId.orden " and have executed this query:
db.getCollection('aplicaciones').find({}).sort({'campoId.orden' : -1})
but I do not get the order I want.
can anyone suggest me a way?
In your documents orden in nested array, so you should use mongo aggregation. So below step will follow :
1> First check campoId exits or not $exists
2> Then unwind campoId array $unwind
3> Then group all fields $group
So query as below :
db.aplicaciones.aggregate({
"$match": {
"campoId": {
"$exists": true // check here campoId presents or not using exists
}
}
}, {
"$unwind": "$campoId" // unwind campoId array
}, {
"$sort": {
"campoId.orden": -1
}
},
//groups all fields
{
"$group": {
"_id": "$_id",
"coleccion": {
"$first": "$coleccion"
},
"nombre": {
"$first": "$nombre"
},
"descripcion": {
"$first": "$descripcion"
},
"tipo": {
"$first": "$tipo"
},
"campoId": {
"$push": "$campoId"
}
}
}).pretty()
I'm trying to learn json in php. Here's my json result from a ElasticSearch query.
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : null,
"hits" : [ {
"_index" : "xenforo",
"_type" : "post",
"_id" : "1816069",
"_score" : null,
"sort" : [ 1365037907 ]
} ]
}
}
I assume that my php code will look something like this:
$myData = json_decode($result);
foreach($myData->hits as $var) {
$post_id[] = $var->_id;
}
Been looking for the answer for hours, I sure do appreciate any help. Thank you.
Edit: Here is the answer:
foreach($myData->hits->hits as $var) {
$post_id[] = $var->_id;
}
You're one ->hits short if you look at your JSON structure...
{
"hits" : {
"hits" : [ {
"_id" : "1816069",
$myData = json_decode($result);
foreach($myData->hits->hits as $hit) {
$post_id[] = $hit->_id;
}
I understand how to parse json with PHP, however I don't understand how to read it with the eye. Can someone please help me understanad this?
Here is my code
<?php
$json = file_get_contents('json.txt');
$json_output = json_decode($json);
foreach ( $json_output->query as $stf )
{
echo "{$stf->response->domains->name}\n";
}
?>
Here is a sample of the json result
{ "query" : { "host" : "test.com",
"tool" : "pro"
},
"response" : { "domain_count" : "13",
"domains" : [ { "last_resolved" : "2012-01-11",
"name" : "test1.com"
},
{ "last_resolved" : "2012-01-11",
"name" : "test2.com"
},
As you can see I tried query->response->domains->name and it didn't work.
How would I tried name?
Thank you in advance
query->response->domains is an indexed array, so you need to get an index, say [0], and then get the ->name from that.
echo $stf->response->domains[0]->name."\n";
foreach ( $json_output->query->response->domains as $domain )
{
echo $domain->name;
}
Study this http://json.org/
If you're trying to read it by eye, it might help to reformat:
{
"query" : {
"host" : "test.com",
"tool" : "pro"
},
"response" : {
"domain_count" : "13",
"domains" : [{
"last_resolved" : "2012-01-11",
"name" : "test1.com"
},{
"last_resolved" : "2012-01-11",
"name" : "test2.com"
}]
}
}