In a php script I have the following
$jsonurlpers = "https://api.openarch.nl/1.1/records/show.json?archive=nha&identifier=8554bfba-9fd9-4ca2-876c-feb7da095d6c";
$jsondatapers = file_get_contents($jsonurlpers);
That string I want to decode with the next command
$jsonpers = json_decode($jsondatapers ,true)[0];
But somewhere it did not work...
When I go to to the #jsonulpers I see :
Event
EventType "Overlijden"
EventDate
Year "1901"
Month "7"
Day "7"
EventPlace
Place "Egmond-Binnen"
RelationEP
Can some one tell me how I can read the value behind EventType in a variable? I tried different options but none works...
Thanks,
Fred
I did try this
e.g.
$itemevent = $jsonpers['Event'];
$itemeventtype = $itemevent['EventType']);
abs I thought
$itemeventtype would have a value of "Overlijden", but it was empty
The (json-)output from the url you gave looks like this:
{
"Event" : {
"EventType" : "Overlijden",
"EventDate" : {
"Month" : "7",
"Day" : "7",
"Year" : "1901"
},
"EventPlace" : {
"Place" : "Egmond-Binnen"
}
},
"RelationEP" : [
{
"RelationType" : "Overledene",
"EventKeyRef" : "Event1",
"PersonKeyRef" : "Person1"
},
{
"PersonKeyRef" : "Person2",
"RelationType" : "Vader",
"EventKeyRef" : "Event1"
},
{
"PersonKeyRef" : "Person3",
"RelationType" : "Moeder",
"EventKeyRef" : "Event1"
}
],
"Source" : {
"SourcePlace" : {
"Place" : "Egmond-Binnen / Egmond-Binnen"
},
"SourceAvailableScans" : {
"Scan" : {
"Uri" : "https://nha.blob.core.windows.net/scans/BS%20Overlijden/Egmond-Binnen/1901/RNH_O_EGB_1901_006-a.jpg",
"OrderSequenceNumber" : "1"
}
},
"SourceLastChangeDate" : "2015-07-31",
"SourceRemark" : {
"Value" : "Datadump ExportBS+Overlijden_20210302_102010.csv van NHA via e-mail"
},
"SourceReference" : {
"InstitutionName" : "Noord-Hollands Archief",
"DocumentNumber" : "14",
"Place" : "Haarlem"
},
"RecordGUID" : "{8554bfba-9fd9-4ca2-876c-feb7da095d6c}",
"SourceIndexDate" : {
"To" : "1901-12-31",
"From" : "1901-01-01"
},
"SourceDate" : {
"Year" : "1901",
"Month" : "7",
"Day" : "8"
},
"SourceType" : "BS Overlijden"
},
"Person" : [
{
"BirthPlace" : {
"Place" : "Egmond-Binnen"
},
"PersonName" : {
"PersonNameLastName" : "Baltus",
"PersonNameFirstName" : "Aafje"
},
"Gender" : "Vrouw",
"Age" : {
"PersonAgeYears" : "8 maanden"
}
},
{
"PersonName" : {
"PersonNameFirstName" : "Jan",
"PersonNameLastName" : "Baltus"
}
},
{
"PersonName" : {
"PersonNameLastName" : "Kuijper",
"PersonNameFirstName" : "Grietje"
}
}
]
}
After "Person":, you see a [, which means there is an array of Persons.
The first person does have a "BirthPlace", for the other persons it seems to be unknown what the "BirthPlace" is, because it is not mentioned in the json.
print($jsonpers["Person"][0]["PersonName"]["PersonFirstName"] should be: "Aafje"
and
print($jsonpers["Person"][2]["PersonName"]["PersonFirstName"] should be: "Grietje"
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 new in mongoDB so I need some help with this.
I have a collection for back up. It contains documents with following data:
{
"_id" : ObjectId("59f18942e7798954dc373b67"),
"category" : "building",
},
{
"_id" : ObjectId("59f18942e7798954dc373b68"),
"category" : "building",
},
{
"_id" : ObjectId("59f18942e7798954dc373b69"),
"category" : "tree",
"type" : 18,
},
{
"_id" : ObjectId("59f18942e7798954dc373b70"),
"category" : "book",
"type" : 18,
}
Then, for example, some of objects were changed and became like that:
{
"_id" : ObjectId("59f18942e7798954dc373b67"),
"category" : "building",
"size" : 100
},
{
"_id" : ObjectId("59f18942e7798954dc373b68"),
"category" : "building",
"size" : 400
},
{
"_id" : ObjectId("59f18942e7798954dc373b70"),
"category" : "book",
"type" : 18,
"title" : "New"
}
So I need to update them in my DB. Just replace, as I understood. I've read the docs about using 'upsert' and 'multi'. But I can't implement it.
How to do that?
Basic syntax for updating document in mongo is
db.collection.update(query,update,options);
For your question,
1.
db.collection.update({_id: ObjectId("59f18942e7798954dc373b67")},{$set:{size:100}});
2.
db.collection.update({_id: ObjectId("59f18942e7798954dc373b68")},{$set:{size:400}});
3.db.collection.update({_id: ObjectId("59f18942e7798954dc373b70")},{$set:{title:"New"}});
EDIT:
You can try out this, If you want to perform Write operation in one command:
> db.characters.bulkWrite(
> [
> { updateOne :
> {
> "filter" : { _id : ObjectId("59f18942e7798954dc373b67") },
> "update" : { $set : { size : 100 } }
> }
> },
> { updateOne :
> {
> "filter" : { _id : ObjectId("59f18942e7798954dc373b68") },
> "update" : { $set : { size : 400 } }
> }
> },
> { updateOne :
> {
> "filter" : { _id : ObjectId("59f18942e7798954dc373b68") },
> "update" : { $set: { title: "New"} }
> }
> },
> ]
>);
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'm working on a membership administration program, for wich we want to use Elasticsearch as search engine. At this point we're having problems with indexing certain fields, because they generate an 'immense term'-error on the _all field.
Our settings:
curl -XGET 'http://localhost:9200/my_index?pretty=true'
{
"my_index" : {
"aliases" : { },
"mappings" : {
"Memberships" : {
"_all" : {
"analyzer" : "keylower"
},
"properties" : {
"Amount" : {
"type" : "float"
},
"Members" : {
"type" : "nested",
"properties" : {
"Startdate membership" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"Enddate membership" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"Members" : {
"type" : "string",
"analyzer" : "keylower"
}
}
},
"Membership name" : {
"type" : "string",
"analyzer" : "keylower"
},
"Description" : {
"type" : "string",
"analyzer" : "keylower"
},
"elementId" : {
"type" : "integer"
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1441310632366",
"number_of_shards" : "1",
"analysis" : {
"filter" : {
"my_char_filter" : {
"type" : "asciifolding",
"preserve_original" : "true"
}
},
"analyzer" : {
"keylower" : {
"filter" : [ "lowercase", "my_char_filter" ],
"tokenizer" : "keyword"
}
}
},
"number_of_replicas" : "1",
"version" : {
"created" : "1040599"
},
"uuid" : "nn16-9cTQ7Gn9NMBlFxHsw"
}
},
"warmers" : { }
}
}
We use the keylower-analyzer, because we don't want the fullname to be split on whitespace. This is because we want to be able to search on 'john johnson' in the _all field as well as in the 'Members'-field.
The 'Members'-field can contain multiple members, wich is where the problems start. When the field only contains a couple of members (as in the example below), there is no problem. However, the field may contain hundreds or thousands of members, wich is when we get the immens term error.
curl 'http://localhost:9200/my_index/_search?pretty=true&q=*:*'
{
"took":1,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"failed":0
},
"hits":{
"total":1,
"max_score":1.0,
"hits":[
{
"_index":"my_index",
"_type":"Memberships",
"_id":"15",
"_score":1.0,
"_source":{
"elementId":[
"15"
],
"Membership name":[
"My membership"
],
"Amount":[
"100"
],
"Description":[
"This is the description."
],
"Members":[
{
"Members":"John Johnson",
"Startdate membership":"2015-01-09",
"Enddate membership":"2015-09-03"
},
{
"Members":"Pete Peterson",
"Startdate membership":"2015-09-09"
},
{
"Members":"Santa Claus",
"Startdate membership":"2015-09-16"
}
]
}
}
]
}
}
NOTE: The above example works! It's only when the field 'Members' contains (a lot) more members that we get the error. The error we get is:
"error":"IllegalArgumentException[Document contains at least one
immense term in field=\"_all\" (whose UTF8 encoding is longer than the
max length 32766), all of which were skipped. Please correct the
analyzer to not produce such terms. The prefix of the first immense
term is: '[...]...', original message: bytes can be at most 32766 in
length; got 106807]; nested: MaxBytesLengthExceededException[bytes can
be at most 32766 in length; got 106807]; " "status":500
We only get this error on the _all-field, not on the original Members-field. With ignore_above, it's not possible to search in the _all field on fullname anymore. With the standard analyzer, i would find this document if i would search on 'Santa Johnson', because the _all-fields has a token 'Santa' and 'Johnson'. That's why i use keylower for these fields.
What i would like is an analyzer that tokenizes on field, but doesn't break up the values in the fields itself. What happens now, is that the entire field 'Members' is being fed as one token, including the childfields. (so, the token in the example above would be:
John Johnson 2015-01-09 2015-09-03 Pete Peterson 2015-09-09 Santa Claus 2015-09-16
Is it possible to tokenize these fields in such a way that every field is being fed to _all as separate tokens, but without breaking up the values in the fields themself? So that the tokens would be:
John Johnson
2015-01-09
2015-09-03
Pete Peterson
2015-09-09
Santa Claus
2015-09-16
Note: We use the Elasticsearch php library.
There is a much better way of doing this. Whether or not the phrase search can span multiple field values is determined by position_offset_gap (in 2.0 it will be renamed into position_increment_gap). This parameter basically specifies how many words/positions should be "inserted" between the last token of one field and the first token of the following fields. By default, in elasticsearch prior to 2.0 position_increment_gap has value of 0. That's is what causing the issues that you describe.
By combining copy_to feature and specifying position_increment_gap you can create an alternative my_all field that will not have this issue. By setting this new field in index.query.default_field setting you can tell elasticsearch to use this field by default instead of _all field when no fields are specified.
curl -XDELETE "localhost:9200/test-idx?pretty"
curl -XPUT "localhost:9200/test-idx?pretty" -d '{
"settings" :{
"index": {
"number_of_shards": 1,
"number_of_replicas": 0,
"query.default_field": "my_all"
}
},
"mappings": {
"doc": {
"_all" : {
"enabled" : false
},
"properties": {
"Members" : {
"type" : "nested",
"properties" : {
"Startdate membership" : {
"type" : "date",
"format" : "dateOptionalTime",
"copy_to": "my_all"
},
"Enddate membership" : {
"type" : "date",
"format" : "dateOptionalTime",
"copy_to": "my_all"
},
"Members" : {
"type" : "string",
"analyzer" : "standard",
"copy_to": "my_all"
}
}
},
"my_all" : {
"type": "string",
"position_offset_gap": 256
}
}
}
}
}'
curl -XPUT "localhost:9200/test-idx/doc/1?pretty" -d '{
"Members": [{
"Members": "John Johnson",
"Startdate membership": "2015-01-09",
"Enddate membership": "2015-09-03"
}, {
"Members": "Pete Peterson",
"Startdate membership": "2015-09-09"
}, {
"Members": "Santa Claus",
"Startdate membership": "2015-09-16"
}]
}'
curl -XPOST "localhost:9200/test-idx/_refresh?pretty"
echo
echo "Should return one hit"
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
"query": {
"match_phrase" : {
"my_all" : "John Johnson"
}
}
}'
echo
echo "Should return one hit"
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
"query": {
"query_string" : {
"query" : "\"John Johnson\""
}
}
}'
echo
echo "Should return no hits"
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
"query": {
"match_phrase" : {
"my_all" : "Johnson 2015-01-09"
}
}
}'
echo
echo "Should return no hits"
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
"query": {
"query_string" : {
"query" : "\"Johnson 2015-01-09\""
}
}
}'
echo
echo "Should return no hits"
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
"query": {
"match_phrase" : {
"my_all" : "Johnson Pete"
}
}
}'
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()