0

I have a JSON data get from an API and I want to get/access for each AttributeValues the code and values to store into the tables, code as tables columns and values are dynamic for each record. I don't need to store the Attachements and Total from the json values. i want to store to tables as an insert into (ID, TrackingNumber, CustomerFull) values (json values). any codeigniter, or php soluton or hint will be much appricate.

 {
    "Values": [{
            "AttributeValues": [{
                    "ID": 0,
                    "Name": "ID",
                    "Values": [
                        "720"
                    ],
                    "DataType": "Number",
                    "Code": "ID",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Tracking Number",
                    "Values": [
                        "xxxx/1094/180730/1"
                    ],
                    "DataType": "String",
                    "Code": "TrackingNumber",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Customer Full",
                    "Values": [
                        "Ali Abdi"
                    ],
                    "DataType": "String",
                    "Code": "CustomerFull",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                }

            ],
            "Attachements": null
        },
        {
            "AttributeValues": [{
                    "ID": 0,
                    "Name": "ID",
                    "Values": [
                        "757"
                    ],
                    "DataType": "Number",
                    "Code": "ID",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Tracking Number",
                    "Values": [
                        "xxx/1094/180731/1"
                    ],
                    "DataType": "String",
                    "Code": "TrackingNumber",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Customer Full",
                    "Values": [
                        "Aberash Haile"
                    ],
                    "DataType": "String",
                    "Code": "CustomerFull",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Service Code",
                    "Values": [
                        "SO-1096"
                    ],
                    "DataType": "String",
                    "Code": "ServiceCode",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                },
                {
                    "ID": 0,
                    "Name": "Request Date",
                    "Values": [
                        "7/31/2018 11:04:06 AM"
                    ],
                    "DataType": "Datetime",
                    "Code": "RequestDate",
                    "IsVisible": true,
                    "IsValueEmptyOrNull": false,
                    "IsDefault": true
                }
            ],
            "Attachements": null
        }
    ],
    "Total": 335
}

1 Answers1

1

First of all, you should convert your JSON to array then use loop(foreach, for, while...) and get value accordingly like this:

<?php 

 $json = '{
  "Values":[
    {
        "AttributeValues":[
            {
            "ID":0,
            "Name":"ID",
            "Values":[
                "720"
            ],
            "DataType":"Number",
            "Code":"ID",
            "IsVisible":true,
            "IsValueEmptyOrNull":false,
            "IsDefault":true
            },
            {
            "ID":0,
            "Name":"Tracking Number",
            "Values":[
                "xxxx/1094/180730/1"
            ],
            "DataType":"String",
            "Code":"TrackingNumber",
            "IsVisible":true,
            "IsValueEmptyOrNull":false,
            "IsDefault":true
            },
            {
            "ID":0,
            "Name":"Customer Full",
            "Values":[
                "Ali Abdi"
            ],
            "DataType":"String",
            "Code":"CustomerFull",
            "IsVisible":true,
            "IsValueEmptyOrNull":false,
            "IsDefault":true
            }
        ],
        "Attachements":null
    },
    {
        "AttributeValues":[
            {
            "ID":0,
            "Name":"ID",
            "Values":[
                "757"
            ],
            "DataType":"Number",
            "Code":"ID",
            "IsVisible":true,
            "IsValueEmptyOrNull":false,
            "IsDefault":true
            },
            {
            "ID":0,
            "Name":"Tracking Number",
            "Values":[
                "xxx/1094/180731/1"
            ],
            "DataType":"String",
            "Code":"TrackingNumber",
            "IsVisible":true,
            "IsValueEmptyOrNull":false,
            "IsDefault":true
            },
            {
            "ID":0,
            "Name":"Customer Full",
            "Values":[
                "Aberash Haile"
            ],
            "DataType":"String",
            "Code":"CustomerFull",
            "IsVisible":true,
            "IsValueEmptyOrNull":false,
            "IsDefault":true
            },
            {
            "ID":0,
            "Name":"Service Code",
            "Values":[
                "SO-1096"
            ],
            "DataType":"String",
            "Code":"ServiceCode",
            "IsVisible":true,
            "IsValueEmptyOrNull":false,
            "IsDefault":true
            },
            {
            "ID":0,
            "Name":"Request Date",
            "Values":[
                "7/31/2018 11:04:06 AM"
            ],
            "DataType":"Datetime",
            "Code":"RequestDate",
            "IsVisible":true,
            "IsValueEmptyOrNull":false,
            "IsDefault":true
            }
        ],
        "Attachements":null
    }
],
"Total":335
}';

$json_data = json_decode($json, true);
//echo "<pre>"; print_r($json_data['Values']); exit;
if(isset($json_data['Values'])){
    foreach($json_data as $data){
        foreach($data as $attr){
            if(isset($attr['AttributeValues'])){
                foreach($attr['AttributeValues'] as $value){
                    if(isset($value['Values'])){
                        foreach($value['Values'] as $val){
                            $attribute_values = $val;
                            echo "<pre>"; print_r($attribute_values); echo "</pre>";
                        }
                    } 
                }
            }
        }
    }
}
?>
Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23
  • thank you so much Rajeev for your swift replay and more or less u my full need but, i hav small change to the output since I want to store it into database I want to see the output as follow 720, xxxx/1094/180730/1, Ali Abdi 757, xxx/1094/180731/1, Aberash Haile, SO-1096, 7/31/2018 11:04:06 AM – Girmangus Hailu Sep 04 '21 at 11:24
  • Now, you can access here the full data, so pick the value that you want to store and set it in the insert query. – Rajeev Singh Sep 04 '21 at 11:31
  • yes, i can access it but can u pls give me a hint to change it to the inset valus(). thank you for ur time – Girmangus Hailu Sep 04 '21 at 11:34
  • echo "
    "; print_r($attribute_values); echo "
    "; that place you can put your insert query
    – Rajeev Singh Sep 04 '21 at 11:35
  • the output of echo "
    "; print_r($attribute_values); echo "
    "; is a single cell value[one column] not for every column of the table.
    – Girmangus Hailu Sep 04 '21 at 12:07
  • This is an example, in above I have used 4 for each loop so you can print value in every step. More more things you are getting full data in array formate like $json_data you can manage accordingly. – Rajeev Singh Sep 05 '21 at 06:39
  • If you are not getting data from array, you should open school site and read more about array and loops – Rajeev Singh Sep 05 '21 at 06:40
  • One more things you can avoid last foreach loop and direct print $value['Values'] or $value['Values'][0] – Rajeev Singh Sep 05 '21 at 06:43