SurveyMonkey: Filter get_survey_details response - php

I'm trying to retrieve the responses for a particular survey from Survey Monkey using the get_responses endpoint which only retrieves the "col" and "row" ids.
{
"status": 0,
"data": [
{
"respondent_id": "4165059283",
"questions": [
{
"answers": [
{
"text": "04/11/1990",
"row": "9333771686"
}
],
"question_id": "842633241"
},
{
"answers": [
{
"row": "9333771693"
},
{
"text": "aa",
"row": "9333771691"
}
],
"question_id": "842633242"
},
{
"answers": [
{
"col": "9333771714",
"row": "9333771713"
},
{
"col": "9333771717",
"row": "9333771712"
},
{
"col": "9333771714",
"row": "9333771711"
},
{
"col": "9333771717",
"row": "9333771710"
},
{
"col": "9333771717",
"row": "9333771709"
},
{
"col": "9333771716",
"row": "9333771708"
},
{
"col": "9333771714",
"row": "9333771707"
},
{
"col": "9333771716",
"row": "9333771706"
},
{
"col": "9333771716",
"row": "9333771705"
},
{
"col": "9333771715",
"row": "9333771704"
},
{
"col": "9333771715",
"row": "9333771703"
},
{
"col": "9333771714",
"row": "9333771702"
},
{
"text": "aa",
"row": "0"
}
],
"question_id": "842633243"
},
{
"answers": [
{
"col": "9333771729",
"row": "9333771726"
},
{
"col": "9333771727",
"row": "9333771725"
},
{
"col": "9333771727",
"row": "9333771724"
},
{
"text": "aaa",
"row": "0"
}
],
"question_id": "842633244"
},
]
}
]
}
I can see that these id's correlate to a question and answer from the get_survey_details endpoint..
But how on earth do you filter the get_survey_details response to only show the question/answer that the respondent has given rather than retrieve everything? I've tried adding to the $params in the class as shown but the request still gives everything back... so clearly this isn't possible or I'm going about it in completely the wrong way:
public function getSurveyDetails($surveyId){
//original $params
//$params = array('survey_id'=>$surveyId);
$params = array(
'survey_id'=>$surveyId,
'pages'=>array(
'questions' => array (
'answers' => array (
'answer_id' => '9333771686',
)
),
)
);
return $this->run('surveys/get_survey_details', $params);
}

Related

Compare 2 Multidimensional Arrays in PHP and show all the difference in an array

I have 2 Multiple-Arrays and I want to compare them. In the second Array I want then show with colors which value has changed, which key is new and which key is removed.
I have found a lot of different solutions, but no one function for me.
Here are my arrays:
{
"originFormation": {
"stationName": "Bern",
"sections": [{
"name": "B",
"cars": [{
"type": "LOK",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": false,
"nextPassage": false
}]
}, {
"name": "C",
"cars": [{
"type": "CAR",
"number": "1",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": ["AbteilRollstuhl", "AbteilBusiness"],
"closed": false,
"previousPassage": false,
"nextPassage": true
}, {
"type": "CAR",
"number": "1",
"class": "2",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": true,
"nextPassage": true
}]
}],
"legendItems": [{
"id": "1",
"text": "1st class coach",
"refersTo": "type"
}, {
"id": "2",
"text": "2st class coach",
"refersTo": "type"
}, {
"id": "Fahrtrichtung",
"text": "Direction of travel",
"refersTo": "code"
}, {
"id": "FA",
"text": "Family Coach with play area",
"refersTo": "type"
}, {
"id": "AbteilRollstuhl",
"text": "Wheelchair space",
"refersTo": "code"
}, {
"id": "AbteilVeloPl",
"text": "Bicycle loading: Without reservation",
"refersTo": "code"
}, {
"id": "LK",
"text": "Locomotive",
"refersTo": "type"
}, {
"id": "WR",
"text": "Restaurant 1st and 2nd class",
"refersTo": "type"
}, {
"id": "AbteilBusiness",
"text": "Business zone in 1st class: Reservation possible",
"refersTo": "code"
},
{
"id": "HFS_WG_NUM",
"text": "Coach",
"refersTo": "code"
}, {
"id": "KD",
"text": "No passage",
"refersTo": "type"
}
],
"info": {
"title": "Changes to the composition of the train",
"formationChanges": []
}
}
}
Array2:
{
"originFormation": {
"stationName": "Bern",
"sections": [
{
"name": "B",
"cars": [
{
"type": "LOK",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": false,
"nextPassage": false
}
]
},
{
"name": "C",
"cars": [
{
"type": "CAR",
"number": "12",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": [
"AbteilRollstuhl",
"AbteilBusiness"
],
"closed": false,
"previousPassage": false,
"nextPassage": true
},
{
"type": "CAR",
"number": "1",
"class": "2",
"occupancy": "UNKNOWN",
"attributes": [
"AbteilStefan"
],
"closed": false,
"previousPassage": true,
"nextPassage": true
},
{
"type": "CAR",
"number": "2",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": true,
"nextPassage": true
}
]
}
],
"legendItems": [
{
"id": "1",
"text": "1st class coach",
"refersTo": "type"
},
{
"id": "2",
"text": "2st class coach",
"refersTo": "type"
},
{
"id": "Fahrtrichtung",
"text": "Direction of travel",
"refersTo": "code"
},
{
"id": "FA",
"text": "Family Coach with play area",
"refersTo": "type"
},
{
"id": "AbteilRollstuhl",
"text": "Wheelchair space",
"refersTo": "code"
},
{
"id": "AbteilVeloPl",
"text": "Bicycle loading: Without reservation",
"refersTo": "code"
},
{
"id": "LK",
"text": "Locomotive",
"refersTo": "type"
},
{
"id": "WR",
"text": "Restaurant 1st and 2nd class",
"refersTo": "type"
},
{
"id": "AbteilBusiness",
"text": "Business zone in 1st class: Reservation possible",
"refersTo": "code"
},
{
"id": "AbteilStefan",
"text": "Ich bin ein neues Abteil",
"refersTo": "code"
},
{
"id": "HFS_WG_NUM",
"text": "Coach",
"refersTo": "code"
},
{
"id": "KD",
"text": "No passage",
"refersTo": "type"
}
],
"info": {
"title": "Changes to the composition of the train",
"formationChanges": [
"All Changed"
]
}
}
}
My fonction to compare:
function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!isset($array2[$key]))
{
$difference[$key] = $value;
}
elseif(!is_array($array2[$key]))
{
$difference[$key] = "<b>".$value."</b>";
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value)
{
$difference[$key] = "<b>".$value."</b>";
}
}
return !isset($difference) ? 0 : $difference;
}
var_dump(array_diff_assoc_recursive($array2, $array1));
This difference i get:
[originformation][sections][1][cars][0][number] -> newValue 12
[originformation][sections][1][cars][1][attributes] -> newValue AbteilStefan
[originformation][sections][1][cars][2] -> all Attributes are new
[originformation][legendItems][9][id] -> newValue AbteilStefan
[originformation][legendItems][9][text] -> newValue Ich bin ein neues Abteil
[originformation][legendItems][10] -> all Attributes are new
[originformation][legendItems][11] -> all Attributes are new
[originformation][info][formationChanges][0] => newValue All Changed
The difference i expected
[originformation][sections][1][cars][0][number] -> newValue 12
[originformation][sections][1][cars][1] -> all Attributes are new
[originformation][legendItems][9] -> all Attributes are new
[originformation][info][formationChanges][0] => newValue All Changed
Problem
My function doesn't see, that car in Array2 I put in a complete new car and also a complete new legendItem and the rest is still the same.
Does someone know how I can compare the arrays, that he doesn't check the order of the attributes on the same level?

Getting REST API results in PHP

I'm trying to get a specific value out of a JSON file in PHP how could this be done. I'm trying to find the value of "allTests": [{"TestOnBlazemeterWasBroken": true}] in the code below and check if it is there and if it equals to true. the value can be true or false and 'TestOnBlazemeterWasBroken' can be also 'TestOnBlazemeterFailed' or 'TestOnBlazemeterWasSuccessful' it depends on the success of an API test
The json file from where i need to find the value "allTests": [{"TestOnBlazemeterWasBroken": true}]
{
"id": "311e6d5a-3f20-42d2-b276-80c5f33f1d7f",
"name": "Testing",
"timestamp": "2021-07-02T10:39:48.544Z",
"collection_id": "6cd6e3b9-cce9-4746-83ca-9349708d3b18",
"folder_id": 0,
"environment_id": "db26e19d-4373-4004-9ad2-348545c6f521",
"totalPass": 1,
"totalFail": 0,
"results": [
{
"id": "bb1b9473-2f66-4153-8996-88cf40715ba6",
"name": "retrieving ID's",
"url": "https://a.blazemeter.com/api/v4/tests?workspaceId=894576&limit=20&hasExecutions=true&filterByActivePlatform=false",
"time": 1116,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
1116
],
"allTests": [
{}
]
},
{
"id": "c694f16e-8195-4696-a874-fdaed51bbfd7",
"name": "Session summary1",
"url": "https://a.blazemeter.com/api/v4/tests/9661550/sessions-summaries",
"time": 259,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
259
],
"allTests": [
{}
]
},
{
"id": "e80d222f-89fe-45cd-a139-6d1b0cfafece",
"name": "Session summary2",
"url": "https://a.blazemeter.com/api/v4/tests/9595495/sessions-summaries",
"time": 442,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
442
],
"allTests": [
{}
]
},
{
"id": "4b129897-0241-465b-94dc-29814808442e",
"name": "Session summary3",
"url": "https://a.blazemeter.com/api/v4/tests/9595540/sessions-summaries",
"time": 319,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
319
],
"allTests": [
{}
]
},
{
"id": "bd17cd20-7165-48e0-8a03-c2d95affc4d1",
"name": "Session summary4",
"url": "https://a.blazemeter.com/api/v4/tests/9595539/sessions-summaries",
"time": 593,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
593
],
"allTests": [
{}
]
},
{
"id": "557c971f-f918-49f1-9d69-7c9547bfe504",
"name": "Session summary5",
"url": "https://a.blazemeter.com/api/v4/tests/9595355/sessions-summaries",
"time": 337,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
337
],
"allTests": [
{}
]
},
{
"id": "8dd94fcc-d04c-43ac-9793-ba0351a41a9d",
"name": "Get master id",
"url": "https://a.blazemeter.com/api/v4/sessions/r-sg-60dc87126dd20190458039",
"time": 259,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
259
],
"allTests": [
{}
]
},
{
"id": "201bd700-de90-45b7-83f6-06b8c7322727",
"name": "See the masters test",
"url": "https://a.blazemeter.com/api/v4/masters/35589479",
"time": 284,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {
"TestOnBlazemeterWasBroken": true
},
"testPassFailCounts": {
"TestOnBlazemeterWasBroken": {
"pass": 1,
"fail": 0
}
},
"times": [
284
],
"allTests": [
{
"TestOnBlazemeterWasBroken": true
}
]
}
],
"count": 1,
"totalTime": 3609,
"collection": {
"requests": [
{
"id": "bb1b9473-2f66-4153-8996-88cf40715ba6",
"method": "GET"
},
{
"id": "c694f16e-8195-4696-a874-fdaed51bbfd7",
"method": "GET"
},
{
"id": "e80d222f-89fe-45cd-a139-6d1b0cfafece",
"method": "GET"
},
{
"id": "4b129897-0241-465b-94dc-29814808442e",
"method": "GET"
},
{
"id": "bd17cd20-7165-48e0-8a03-c2d95affc4d1",
"method": "GET"
},
{
"id": "557c971f-f918-49f1-9d69-7c9547bfe504",
"method": "GET"
},
{
"id": "8dd94fcc-d04c-43ac-9793-ba0351a41a9d",
"method": "GET"
},
{
"id": "201bd700-de90-45b7-83f6-06b8c7322727",
"method": "GET"
}
]
}
}
The part I'm looking for is "allTests": [{"TestOnBlazemeterWasBroken": true}] how could I find this value and check if it is there and if it equals to true.
{
"id": "201bd700-de90-45b7-83f6-06b8c7322727",
"name": "See the masters test",
"url": "https://a.blazemeter.com/api/v4/masters/35589479",
"time": 284,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {
"TestOnBlazemeterWasBroken": true
},
"testPassFailCounts": {
"TestOnBlazemeterWasBroken": {
"pass": 1,
"fail": 0
}
},
"times": [
284
],
"allTests": [
{
"TestOnBlazemeterWasBroken": true
}
]
}
I have tried this but it didn't work - and I get this error:
Fatal error: Uncaught TypeError: Cannot access offset of type string on string in C:\xampp\htdocs\index.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php on line 11
Code:
<?php
// Read JSON file
$json = file_get_contents('Testing.postman_test_run.json');
//Decode JSON
$json_data = json_decode($json,true);
//Traverse array and get the data for students aged less than 20
foreach ($json_data as $key1 => $value1) {
if($json_data[$key1]["allTests"][7]['TestOnBlazemeterWasBroken'] == true){
print_r($json_data[$key1]);
}
}
?>
The following ought to work OK whether there is one or more results.
$str='{
"id": "311e6d5a-3f20-42d2-b276-80c5f33f1d7f",
"name": "Testing",
"timestamp": "2021-07-02T10:39:48.544Z",
"collection_id": "6cd6e3b9-cce9-4746-83ca-9349708d3b18",
"folder_id": 0,
"environment_id": "db26e19d-4373-4004-9ad2-348545c6f521",
"totalPass": 1,
"totalFail": 0,
"results": [
{
"id": "bb1b9473-2f66-4153-8996-88cf40715ba6",
"name": "retrieving IDs",
"url": "https://a.blazemeter.com/api/v4/tests?workspaceId=894576&limit=20&hasExecutions=true&filterByActivePlatform=false",
"time": 1116,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
1116
],
"allTests": [
{}
]
},
{
"id": "201bd700-de90-45b7-83f6-06b8c7322727",
"name": "See the masters test",
"url": "https://a.blazemeter.com/api/v4/masters/35589479",
"time": 284,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {
"TestOnBlazemeterWasBroken": true
},
"testPassFailCounts": {
"TestOnBlazemeterWasBroken": {
"pass": 1,
"fail": 0
}
},
"times": [
284
],
"allTests": [
{
"TestOnBlazemeterWasBroken": true
}
]
}
]
}';
$fail=false;
$testname='TestOnBlazemeterWasBroken';
$json=json_decode( $str );
$results=$json->results ?: false;
if( $results ){
foreach( $results as $result ){
if( property_exists( $result, 'allTests' ) ){
foreach( $result->allTests as $test ){
if( property_exists( $test, $testname ) && $test->$testname==true ){
$fail=true;
break 2;
}
}
}
}
}
$status=$fail ? 'Failed! ' : 'Passed!';
printf( 'The test "%s" was %s', $testname, $status );
Given the above $str data this will yield "The test "TestOnBlazemeterWasBroken" was Failed!" because within one of the results the specific test was found and evaluated to true.
Given the source data ( saved as blaze-test.json )
{
"id": "311e6d5a-3f20-42d2-b276-80c5f33f1d7f",
"name": "Testing",
"timestamp": "2021-07-02T10:39:48.544Z",
"collection_id": "6cd6e3b9-cce9-4746-83ca-9349708d3b18",
"folder_id": 0,
"environment_id": "db26e19d-4373-4004-9ad2-348545c6f521",
"totalPass": 1,
"totalFail": 0,
"results": [
{
"id": "bb1b9473-2f66-4153-8996-88cf40715ba6",
"name": "retrieving ID's",
"url": "https://a.blazemeter.com/api/v4/tests?workspaceId=894576&limit=20&hasExecutions=true&filterByActivePlatform=false",
"time": 1116,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
1116
],
"allTests": [
{}
]
},
{
"id": "c694f16e-8195-4696-a874-fdaed51bbfd7",
"name": "Session summary1",
"url": "https://a.blazemeter.com/api/v4/tests/9661550/sessions-summaries",
"time": 259,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
259
],
"allTests": [
{}
]
},
{
"id": "e80d222f-89fe-45cd-a139-6d1b0cfafece",
"name": "Session summary2",
"url": "https://a.blazemeter.com/api/v4/tests/9595495/sessions-summaries",
"time": 442,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
442
],
"allTests": [
{}
]
},
{
"id": "4b129897-0241-465b-94dc-29814808442e",
"name": "Session summary3",
"url": "https://a.blazemeter.com/api/v4/tests/9595540/sessions-summaries",
"time": 319,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
319
],
"allTests": [
{}
]
},
{
"id": "bd17cd20-7165-48e0-8a03-c2d95affc4d1",
"name": "Session summary4",
"url": "https://a.blazemeter.com/api/v4/tests/9595539/sessions-summaries",
"time": 593,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
593
],
"allTests": [
{}
]
},
{
"id": "557c971f-f918-49f1-9d69-7c9547bfe504",
"name": "Session summary5",
"url": "https://a.blazemeter.com/api/v4/tests/9595355/sessions-summaries",
"time": 337,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
337
],
"allTests": [
{}
]
},
{
"id": "8dd94fcc-d04c-43ac-9793-ba0351a41a9d",
"name": "Get master id",
"url": "https://a.blazemeter.com/api/v4/sessions/r-sg-60dc87126dd20190458039",
"time": 259,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {},
"testPassFailCounts": {},
"times": [
259
],
"allTests": [
{}
]
},
{
"id": "201bd700-de90-45b7-83f6-06b8c7322727",
"name": "See the masters test",
"url": "https://a.blazemeter.com/api/v4/masters/35589479",
"time": 284,
"responseCode": {
"code": 200,
"name": "OK"
},
"tests": {
"TestOnBlazemeterWasBroken": true
},
"testPassFailCounts": {
"TestOnBlazemeterWasBroken": {
"pass": 1,
"fail": 0
}
},
"times": [
284
],
"allTests": [
{
"TestOnBlazemeterWasBroken": true
}
]
}
],
"count": 1,
"totalTime": 3609,
"collection": {
"requests": [
{
"id": "bb1b9473-2f66-4153-8996-88cf40715ba6",
"method": "GET"
},
{
"id": "c694f16e-8195-4696-a874-fdaed51bbfd7",
"method": "GET"
},
{
"id": "e80d222f-89fe-45cd-a139-6d1b0cfafece",
"method": "GET"
},
{
"id": "4b129897-0241-465b-94dc-29814808442e",
"method": "GET"
},
{
"id": "bd17cd20-7165-48e0-8a03-c2d95affc4d1",
"method": "GET"
},
{
"id": "557c971f-f918-49f1-9d69-7c9547bfe504",
"method": "GET"
},
{
"id": "8dd94fcc-d04c-43ac-9793-ba0351a41a9d",
"method": "GET"
},
{
"id": "201bd700-de90-45b7-83f6-06b8c7322727",
"method": "GET"
}
]
}
}
The processing script:
// Fetch the data from local file in same directory
$str=file_get_contents( __DIR__ . '/blaze-test.json' );
// set the test and result status default - assume pass unless specific fail
$fail=false;
$testname='TestOnBlazemeterWasBroken';
// decode the string data so we can work with it
$json=json_decode( $str );
$results=$json->results ?: false;
if( $results ){
//iterate through the results
foreach( $results as $result ){
$name=$result->name;
$code=$result->responseCode->code;
if( property_exists( $result, 'allTests' ) ){
foreach( $result->allTests as $test ){
// does the test exist in the data and was it failed?
if( property_exists( $test, $testname ) && $test->$testname==true ){
$fail=true;
break 2;
}
}
}
}
$status=$fail ? 'Failed' : 'Passed';
printf(
'
<style>
.Failed{color:red}
.Passed{color:green}
</style>
<h2>Tests run: %d</h2>
<p>The test "%s" within "%s" was <span class="%s">%s</span> with code %s!</p>',
count($results),
$testname,
$name,
$status,
$status,
$code
);
}
The result:
Tests run: 8
The test "TestOnBlazemeterWasBroken" within "See the masters test" was Failed with code 200!
You need to look at your data.
First (if you do not see it in json) you may look at the array.
So just print it (f.e. echo var_export($json_data, true) . PHP_EOL; die;)
or write it into an file.
Looks ok? Then build the loop.
First you loop through the array at the result offset.
And if you do not know what you get on one result then do the same as previously: look at it. Print it.
Same for any deeper loops.
The loop:
// Read JSON file
$json = file_get_contents('Testing.postman_test_run.json');
//Decode JSON
$json_data = json_decode($json, true);
//Traverse array and get the data for students aged less than 20
foreach ($json_data['results'] as $result) {
foreach ($result["allTests"] as $test) {
if (isset($test['TestOnBlazemeterWasBroken'])
and $test['TestOnBlazemeterWasBroken'] === true
) {
// You found it and its true.
echo "TestOnBlazemeterWasBroken is true at {$result['id']}\r\n";
// May collect or print or w/e.
}
}
}

Elastic Search bucket list for a ECommerce Shop / Neast Attributes

can you help me please? I have a ecommerce website with 1000+ products. Each product has a bunch of options like "color", "size", and other specs... but i don't know all the attributes. so i define a document with this mapping:
"mappings" : {
"article" : {
"properties": {
"options": {
"type": "nested",
"include_in_parent":"true",
"properties": {
"id": {"type": "string"},
"name": {"type": "string"},
"values": {"type": "string"}
}
}
}
}
And this is my Query to get the Bucket list:
{
"query": {
"bool": {
"must": [
{
"term": {
"categorie_id": "f52330ce2669dfab884c2d60468b8466"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 1,
"sort": [
{
"sorttype": {
"order": "desc"
}
},
"_score"
],
"aggs": {
"baked_goods": {
"nested": {
"path": "options"
},
"aggs": {
"name": {
"terms": {
"field": "id"
},
"aggs": {
"name": {
"terms": {
"field": "values"
}
}
}
}
}
}
}
}
I get Documents, but the Result of the Buckets is Empty...
"aggregations": {
"baked_goods": {
"doc_count": 3331,
"name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [ ]
}
}
}
i want something like:
"color" => "red" (4)
"color" => "blue" (2)
"size" => "X" (11)
..
Can you please help me??
i found a solution.
Mapping:
"options": {
"type": "nested",
"include_in_parent": true,
"properties": {
"name": { "type": "text" , "analyzer": "whitespace", "fielddata": true},
"values": { "type": "text" , "analyzer": "whitespace", "fielddata": true}
}
}
Query:
"aggs": {
"facets": {
"nested": {
"path": "options"
},
"aggs": {
"name": {
"terms": {
"field": "options.name"
},
"aggs": {
"name": {
"terms": {
"field": "options.values"
}
}
}
}
}
} }

Number format Exception For string type

I have a mapping like this
{
"settings": {
"analysis": {
"filter": {
"nGramFilter": {
"type": "nGram",
"min_gram": 3,
"max_gram": 20,
"token_chars": [
"letter",
"digit",
"punctuation",
"symbol"
]
},
"email" : {
"type" : "pattern_capture",
"preserve_original" : 1,
"patterns" : [
"([^#]+)",
"(\\p{L}+)",
"(\\d+)",
"#(.+)"
]
},
"number" : {
"type" : "pattern_capture",
"preserve_original" : 1,
"patterns" : [
"([^+-]+)",
"(\\d+)"
]
},
"edgeNGramFilter": {
"type": "nGram",
"min_gram": 1,
"max_gram": 10,
"token_chars": [
"letter",
"digit",
"punctuation",
"symbol"
]
}
},
"analyzer": {
"nGramAnalyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"nGramFilter"
]
},
"whitespaceAnalyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase"
]
},
"email" : {
"tokenizer" : "uax_url_email",
"filter" : [
"email",
"lowercase",
"unique"
]
},
"number" : {
"tokenizer" : "whitespace",
"filter" : [ "number", "unique" ]
},
"edgeNGramAnalyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"edgeNGramFilter"
]
}
}
}
},
"users": {
"mappings": {
"user_profiles": {
"properties": {
"firstName": {
"type": "string",
"analyzer": "nGramAnalyzer",
"search_analyzer": "whitespaceAnalyzer"
},
"lastName": {
"type": "string",
"analyzer": "nGramAnalyzer",
"search_analyzer": "whitespaceAnalyzer"
},
"email": {
"type": "string",
"analyzer": "email",
"search_analyzer": "whitespaceAnalyzer"
},
"score" : {
"type": "string"
},
"homeLandline": {
"type": "string",
"analyzer": "number",
"search_analyzer": "whitespaceAnalyzer"
},
"dob": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"mobile": {
"type": "integer"
},
"residenceCity": {
"type": "string",
"analyzer": "edgeNGramAnalyzer",
"search_analyzer": "whitespaceAnalyzer"
},
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
}
}
}
}
}
I can get the score as integer as well as "NA" so I mapped the type as string but while posting data to the index i am getting Number Format Exception.
For Example:
if I post first data as integer and followed by "NA". I am getting these exception.
while checking my log file I am getting this errors:
[2016-08-29 15:19:01] elasticlog.WARNING: Response ["{\"error\":{\"root_cause\":[{\"type\":\"mapper_parsing_exception\",\"reason\":\"failed
to parse
[score]\"}],\"type\":\"mapper_parsing_exception\",\"reason\":\"failed
to parse
[score]\",\"caused_by\":{\"type\":\"number_format_exception\",\"reason\":\"For
input string: \"NH\"\"}},\"status\":400}"] []
Your mapping is incorrect. It should be, assuming, users is the index name and user_profiles is the type:
{
"users": {
"mappings": {
"user_profiles": {
"properties": {
"score": {
"type": "string"
}
}
}
}
}
}
You have a missing mappings before user_profiles.

How can I parse JSON file in PHP with structure like this

How can I parsing JSON file in PHP with structure like this.
{
"url": "http://lotto.mthai.com",
"result": {
"extractorData": {
"url": "http://lotto.mthai.com",
"resourceId": "85407efc3b77dc0c03350101dbe4d644",
"data": [
{
"group": [
{
"title": [
{
"text": "รางวัลที่ 1"
}
],
"number": [
{
"text": "511825"
}
]
},
{
"title": [
{
"text": "เลขท้าย 2 ตัว"
}
],
"number": [
{
"text": "14"
}
]
},
{
"title": [
{
"text": "รางวัลเลขหน้า 3 ตัว"
}
],
"number": [
{
"text": "111 775"
}
]
},
{
"title": [
{
"text": "รางวัลเลขท้าย 3 ตัว"
}
],
"number": [
{
"text": "880 937"
}
]
},
{
"title": [
{
"text": "รางวัลข้างเคียงรางวัลที่ 1"
}
],
"number": [
{
"text": "511824 511826"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล รางวัลที่ 2"
}
],
"number": [
{
"text": "041316 051696 174632 262383 301461"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 3"
}
],
"number": [
{
"text": "151368 192853 218010 381563 441994 593440 608530 958702 960236 991980"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 4"
}
],
"number": [
{
"text": "005297 143532 183328 308705 457316 561361 692884 813010 891564 984090 027175 151262 203467 309366 464915 580426 725352 835838 895659 986826 034358 160471 207682 426987 477041 626565 727202 840597 927093 990225 055970 175123 221131 428397 542542 674499 761363 883250 936443 994258 100858 175672 285542 434884 558734 692634 795866 888874 967956 996299"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 5"
}
],
"number": [
{
"text": "018823 074535 190371 313865 352942 456747 541518 709349 861423 919589 030122 085212 200070 318968 359324 474167 571715 734617 862093 924308 047376 102785 206781 320576 376900 481372 573928 739412 863966 928572 049905 128721 209050 324031 386258 505554 599073 767614 867382 958601 051129 132159 227324 326875 396408 506582 637094 786639 869522 960148 051242 137750 265952 327702 405928 507898 654254 788560 878668 962359 052418 156993 269239 330428 416971 508844 664963 805061 881503 971836 054447 157814 286374 330762 434541 526775 705692 811851 890915 983191 060979 176311 301301 336489 451863 529673 707115 819033 902456 983528 061270 184563 308537 352516 455643 535855 707396 829248 908825 999541"
}
]
}
]
}
]
},
"pageData": {
"statusCode": 200,
"timestamp": 1464855539981
}
}
}
json file exported from import.io
I want to parsing title and number in group and keep it in array.
I tried many ways but always got error.
Thanks you.
Unless I misunderstood your question you could organize your JSON like below:
"group": [
{
"title": "Some text",
"number": "511825"
},
{
"title": "Some text 2",
"number": "98989"
},

Categories