0

I'll have JSON and try to disassemble and save it in database:

{
    "token": "f56445c24ad84e9c452f3ca6ef91e90beb2aef21",
    "participant": "ZuP0rsny3MhqLC0",
    "data": {
        "orgs": [{
            "org1": {
                "attribute": [{
                    "name": "val",
                    "inn": "val",
                    "kpp": "val"
                }],
                "tickets": [{
                    "ticket1": [{
                        "account_uid": "val",
                        "created_at": "val",
                        "title": "val",
                        "text": "val",
                        "status": "val",
                        "priority": "val",
                        "fullname": "val",
                        "workaddr": "val",
                        "phone": "val",
                        "email": "val",
                        "workstart": "val",
                        "workend": "val",
                        "dispetcher": "val",
                        "responsible": "val",
                        "closed_at": "val",
                        "file": "val"
                    }]
                }]
            }
        }]
    }
}

I'll try to save it to db with using use Illuminate\Http\Request(Request $request), but can't given access to array org1 and ticket.
Misstake:

Undefined array key "org1"

Which way to move? Thnx

2 Answers 2

1

Change the json into an array:

$json_array = json_decode($your_json, true);

Access the element you want:

$org1 = $json_array['data']['orgs']['org1'];

And if you want to convert to json:

$org1 = json_encode($org1);
Sign up to request clarification or add additional context in comments.

10 Comments

Thnx, 4 answer, but using your way i'll have mistake: ErrorException: Trying to access array offset on value of type null
Just tried, it works well on my side. Share your code please
Maybe you should implement some validation in your controller. It seems the array does not exist
json in topick start, and function to try output: look like yor example^
public function test(Request $request) { $json_array = json_decode($request->data, true); $org1 = $json_array['data']['orgs']['org1']; dd ($org1); }
|
0

dd() output:

array:1 [
  0 => array:1 [
    "orgs" => array:1 [
      0 => array:1 [
        "org1" => array:1 [
          0 => array:2 [
            "attribute" => array:1 [
              0 => array:3 [
                "name" => "Из json"
                "inn" => "88888111"
                "kpp" => "11188888"
              ]
            ]
            "tickets" => array:1 [
              0 => array:1 [
                "ticket1" => array:1 [
                  0 => array:14 [
                    "account_uid" => "UKP2010001002"
                    "created_at" => "2021-13-07"
                    "title" => "Из json"
                    "text" => "Из json"
                    "status" => "1"
                    "priority" => "2"
                    "fullname" => "Вася Джейсонович"
                    "workaddr" => "дом улица фонарь"
                    "phone" => "+791478562548"
                    "email" => "[email protected]"
                    "workstart" => "вечером"
                    "workend" => "утром"
                    "dispetcher" => "Матрёна Петровна"
                    "responsible" => "Гайдай Пётр"
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]

Comments

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.