I have this JSON string:
{
"name": "test task1",
"desc": "test desc1",
"id": "1"
}{
"name": "test task1aaaa",
"desc": "test desc1",
"id": "2"
}
But it looks like it's not correct (JSONLint tells me) so PHP's json_decode()can't decode it. There's any way to separate the two JSON arrays into two strings (or into how much string the arrays are) for making json_decode decode them?
Assuming your intention is to have an array of two elements, your JSON should look like:
[
{
"name": "test task1",
"desc": "test desc1",
"id": "1"
},{
"name": "test task1aaaa",
"desc": "test desc1",
"id": "2"
}
]
the most straightforward
$str = ' {
"name": "test task1",
"desc": "test desc1",
"id": "1"
}{
"name": "test task1aaaa",
"desc": "test desc1",
"id": "2"
}';
var_dump(json_decode('['.str_replace('}{','},{',$str).']'));
<?php
$str='{
"name": "test task1",
"desc": "test desc1",
"id": "1"
}{
"name": "test task1aaaa",
"desc": "test desc1",
"id": "2"
}';
$arrays = explode("{", $str);
foreach($arrays as &$arr) $arr='{'.$arr;
//decode
foreach ($arrays as $arr) print_r(json_decode($arr,true));
Related
I have an object that looks like this
{
"Product_1": {
"name": "Product 1",
"price": "300"
},
"Product_2": {
"name": "Product 2",
"price": "300"
},
"Product_3": {
"name": "Product 3",
"price": "300"
},
}
What I would like to know is. Is it possible to grab the keys for example: Product_1, Product_2 without having to loop through it?
Yes you can using the array_keys method like this:
$json = '{
"Product_1": {
"name": "Product 1",
"price": "300"
},
"Product_2": {
"name": "Product 2",
"price": "300"
},
"Product_3": {
"name": "Product 3",
"price": "300"
}
}';
$array = json_decode($json, JSON_OBJECT_AS_ARRAY);
$keys = array_keys($array);
You can get all keys of the object simply by call the function get_object_vars()
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I've got basic array from my api:
{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }
I want to get id value from request for my php variable lets say: $idVariable. How can I do it?
I tried something like:
$idVariable = $request->specialities[0]->id
but it seems not working. What is the right way?
Then how should I work with the arrays of object in this case:
{ "title": "offer title", "body": "offer body",
"specialities": [
{ "lang": "en", "id": "1", "icon": "0", "name": "speciality name 1" },
{ "lang": "en", "id": "2", "icon": "0", "name": "speciality name 2" },
{ "lang": "en", "id": "2", "icon": "0", "name": "speciality name 3" },
etc...], "region": "region1" }
To get id's of every object in specialities array? I know that it could be a duplicate question, but I ask for just a basic example.
I tried to use json decode like below:
json_decode($request->get('specialties'))->id
edit:
The almost-right way to do it is to decode json file first:
$data = json_decode($request);
and then get the right property from the array:
$id = $data['specialities'][0]['id'];
the problem now is that id is a string not an integer and by simply using:
$int_id = intval($id)
I've got $int_id = 0 instead of 1,2,3 etc
You are getting a response from API in JSON you should use json_decode() and then use the data. Try this code.
$json = '{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }';
$data = json_decode($json);
echo $data->specialities->id;
In PHP, how can I make this data:
{
"items": {
"item": [{
"id": "59",
"type": "Domain",
"relid": "27",
"description": "Sample Monthly Product(01\ / 01\ / 2016 - 31\ / 01\ / 2016)",
"amount": "180.00",
"taxed": "0"
}]
}
}
Have this format:
{
"items[item][0][id]": "59",
"items[item][0][type]": "Domain",
"items[item][0][relid]": "27",
"items[item][0][description]": "Sample Monthly Product (01\/01\/2016 - 31\/01\/2016)",
"items[item][0][amount]": "180.00",
"items[item][0][taxed]": "0"
}
Reason: Setting up a Zap (via Zapier) using email parser to import invoices from WHMCS, and it seems to work with parsing data when 2nd format (items[item][0][id]) to read the line descriptions but not when using the 1st. In the WHMCS API ref it shows it outputting as the 2nd format but can't see why mine looks like 1st (developers.whmcs.com/api-reference/getinvoice)
I think this might work with your current setup:
<?php
//assuming your current dataset isn't in JSON, you can ignore this part
$json = '{
"items": {
"item": [{
"id": "59",
"type": "Domain",
"relid": "27",
"description": "Sample Monthly Product",
"amount": "180.00",
"taxed": "0"
},
{
"id": "203",
"type": "Server",
"relid": "86",
"description": "Sample Yearly Product",
"amount": "290.00",
"taxed": "1"
}]
}
}';
$json = json_decode($json, true);
$parsed = array();
foreach ($json['items']['item'] as $index => $item)
foreach ($item as $attr => $val)
$parsed['items[item][' . $index . '][' . $attr . ']'] = $val;
echo json_encode($parsed);
Output:
{
"items[item][0][id]": "59",
"items[item][0][type]": "Domain",
"items[item][0][relid]": "27",
"items[item][0][description]": "Sample Monthly Product",
"items[item][0][amount]": "180.00",
"items[item][0][taxed]": "0",
"items[item][1][id]": "203",
"items[item][1][type]": "Server",
"items[item][1][relid]": "86",
"items[item][1][description]": "Sample Yearly Product",
"items[item][1][amount]": "290.00",
"items[item][1][taxed]": "1"
}
I am trying to take my JSON input and generate a easy array to search in, but it is as far as I can tell giving me some trouble.
First of, this is my JSON as is, loaded from a file:
{
"teams": [
{
"id": "1",
"boat": "Test",
"name": "Palle Test"
},
{
"id": "2",
"boat": "Test 2",
"name": "Name Test 2"
},
{
"id": "3",
"boat": "Test 3",
"name": "Name Test 3"
},
{
"id": "4",
"boat": "Test 4",
"name": "mller"
}
]
}
Is there a way to simplify this or minimise the number of arrays?
I have been using the following code to preview my array, but I am trying to get it to search for id and then print the values for id, boat and name.
$json_url = "teams.json";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo '<pre>';
print_r($data);
echo '</pre>';
This is really quite simple. There is no need to convert a perfectly good object into an array, it only makes life more complex.
The only array you have in your data structure is the teams array and that is easily processed like this
$json = '{
"teams": [
{
"id": "1",
"boat": "Test",
"name": "Palle Test"
},
{
"id": "2",
"boat": "Test 2",
"name": "Name Test 2"
},
{
"id": "3",
"boat": "Test 3",
"name": "Name Test 3"
},
{
"id": "4",
"boat": "Test 4",
"name": "mller"
}
]
}';
$data = json_decode($json);
//print_r($data);
foreach ($data->teams as $object) {
if ( $object->id == 2) {
echo 'boat is ' . $object->boat . ' and name is ' . $object->name . PHP_EOL;
}
}
And the result of the above example would be
boat is Test 2 and name is Name Test 2
I'm Getting values like this in JSON format -
{
"comments": [{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
}
But i don't want first array name means i need result like this -
[{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
Need a help...
Do like this...
<?php
$json='{
"comments": [{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
}';
$arr=json_decode($json,1);
echo json_encode($arr['comments']);
OUTPUT :
[{"name":"ABC","desc":"Hello...","values":[{"status":"fine","label":""}]},{"name":"XYZ","desc":"Good Morning..","values":[{"status":"fine","label":"happy"}]}]
Try this:
$data = '[{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]';
$return = json_decode($data, true);
foreach ($return as $key => $value){
unset($return[$key]['name']);
}
echo '<pre>';
print_r($return);
echo '</pre>';