3

I need to get an item from the json output however the json contents aren't always consistent.

For example if I wanted get the value for "name" it would be ['result']['attributes'][0]['name'];

But in the event that the json is delivered as the second example it would be ['result']['attributes'][1]['name'];

{"result":{
    "attributes":[
        {"user":"newb","name":"mike"},
        {"state":"california","city":"los angeles"}
    ]
}}

{"result":{
    "attributes":[
        {"state":"california","city":"los angeles"},
        {"user":"newb","name":"mike"}
    ]
}}

How would I get the "name" value if the index of the array it's in is unknown?

3
  • Do you have any control over the JSON output? Commented Jun 6, 2012 at 5:18
  • I do not, it's an output from an 3rd party api. Commented Jun 6, 2012 at 5:19
  • 1
    That's unfortunate. I would at least file a bug report with the 3rd party, as the attributes property is not designed very well; instead of an array of bizarrely-grouped attributes, it should just be an object with each attribute declared separately. i.e.: {"result":{"attributes":{"user":"newb","name":"mike","state":"california","city":"los angeles"}}} Commented Jun 6, 2012 at 5:28

2 Answers 2

3
var arr = obj.result.attributes;
for (var i=0; i<arr.length; i++)
    if ("name" in arr[i])
         return arr[i].name;

or, if it's always only the two objects in the array:

 var attrs = obj.result.attributes;
 return attrs["name" in attrs[0] ? 0 : 1].name;

But I would insist on a change in that api, the two objects should be just merged.


EDIT: Sorry, here comes the PHP:

$attrs = json_decode($jsonStr, true)["result"]["attributes"];
return $attrs[ isset($attrs[0]["name"]) ? 0 : 1 ];
Sign up to request clarification or add additional context in comments.

3 Comments

I believe the OP wants a PHP solution--not Javascript.
Uh, why do I always think "JavaScript" when it's highlighted as "JSON"? :-(
Yes, I'm developing in PHP. Sorry for any confusion.
1

This will return the index into the attributes array that contains the object with a name attribute of "mike".

function extractNameFromJson($json) {
    foreach ($json->result->attributes as $i => $attribute) {
        if (isset($attribute['name']) && $attribute['name'] == 'mike') {
            return $i;
        }
    }
}

$index = extractNameFromJson($json);
echo $json->result->attributes[$index]['user'];

==> newb

5 Comments

I need the index of the attribute mike would be in. I am trying to write a function where I can get the value of user where name = mike. That way in the event both attributes contained the same user but different names.
@MikeMills - Is this what you meant?
It's a bit closer to what I need. Lets say in the json there is a key in the attributes called "fieldid" and the value of that is "372", and in every attribute there is a key called "value" and I need to get the value of the "value" key where fieldid == 372.
something like: function getfieldid($json, [fieldid value]){ $djson = json_decode($json, true); //code to find value of key "value" where key "fieldid" == [fieldid value] }
function Getfieldid($json, $fid) { $djson = json_decode($json, true); foreach ($djson['result']['attributes'] as $i => $attribute) { if (isset($attribute['fieldId']) && $attribute['fieldId'] == $fid) { return $djson['result']['attributes'][$i]['value']; } } } It's working now, thanks David :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.