user2691659
user2691659

Reputation:

possible to remove this object from json array?

I have an array that looks like this :

[ 
{"value": {
"api_rev":"1.0",
"type":"router",
"hostname":"Router Hasselt",
"lat":50.9307,
"lon":5.33248,
"elev":50,
"aliases":[
          {
           "type":"wired",
           "alias":"11.96.253.9"
           }],
"community":"Antwerpen",
"attributes":{"firmware":""}}}]"

Is it possible to remove [{"value": and obviously the closing of it at the end }] and leave the rest as it is ? I tried unsetting "value" but this actually removes everything which i understand why. But if there is a workaround i will appreciate !

I expect this :

{
    "api_rev":"1.0",
    "type":"router",
    "hostname":"Router Hasselt",
    "lat":50.9307,
    "lon":5.33248,
    "elev":50,
    "aliases":[
              {
               "type":"wired",
               "alias":"11.96.253.9"
               }],
    "community":"Antwerpen",
    "attributes":{"firmware":""}}"

Upvotes: 0

Views: 120

Answers (3)

Ravi Dhoriya ツ
Ravi Dhoriya ツ

Reputation: 4414

Try this,

<?php 
$str='[ 
    {"value": {
    "api_rev":"1.0",
    "type":"router",
    "hostname":"Router Hasselt",
    "lat":50.9307,
    "lon":5.33248,
    "elev":50,
    "aliases":[
       {
       "type":"wired",
       "alias":"11.96.253.9"
       }],
    "community":"Antwerpen",
    "attributes":{"firmware":""}}}]';

$json=json_decode($str);

echo json_encode($json[0]->value);

?>

Test it here: DEMO

Upvotes: 0

Rohit Subedi
Rohit Subedi

Reputation: 550

This might help :)

$obj = json_decode($json);
echo json_encode($obj[0]->value);

Upvotes: 0

Daniel Cheng
Daniel Cheng

Reputation: 870

For example you have this:

$json = json_decode("your stuff");

Then you do:

$json = $json[0]->value;

Then you can encode it back:

$str = json_encode($json);

Upvotes: 2

Related Questions