1

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.

0

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

6 Comments

I just seen what you added, the last line of code works. Also what about accessing the "buildsByBranchName" section?
I tried the following, but no luck. $version = "origin/release_5.6.0"; echo $test->actions[0]->causes[0]->buildsByBranchName[0]->$version->buildNumber;
buildsByBranchName is not under causes
Still getting an error (Undefined property: stdClass::$buildsByBranchName). Using the following below. $version = "origin/release_5.6.0"; echo $test->actions[0]->buildsByBranchName->$version->buildNumber;
I think I am having an issue because of the empty brackets in the Json above buildsByBranchName.
|

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.