0

I've read through similar posts but still could use some help in my case...I'm having a devil of a time getting values from a json array that contains key and value pairs:

{"address": "4 Ficticious Ave", 
"city": "Miami", 
"country": "United States", 
"email": "[email protected]", 
"first_name": "Jane", 
"last_name": "Doe", 
"state": "FL", 
"zip_code": "03423", 
"response_data": 
"[{"key":"7122", "value":"37-52"},
{"key":"7123","value":"Female"},
{"key":"7124","value":"$35,000 to $50,000 USD"},
{"key":"6176","value":"Miami"},
{"key":"6177","value":"FL"},
{"key":"6179","value":"United States"}]"}

I've tried to get all of the values, but have been unsuccessful particularly with the response_data key|value pairs:

`// GET THE JSON DATA FROM THE $data array

// Identify the content as json
header("Content-Type: application/json; charset=UTF-8");

// get the contents of the JSON file
$data = file_get_contents("php://input");

//decode JSON data to PHP array
$content = json_decode($data, true);

//Fetch the details of customer
$Cust_Fname = $content['first_name'];
$Cust_Lname = $content['last_name'];
$Cust_Email = $content['email'];
$Street_Address = $content['address'];
$City = $content['city'];
$State = $content['state'];
$Country = $content['country'];
$Postal_Code = $content['zip_code'];

//also fetch the appended "array" of key/value fields...
$Response_AgeKey = $content['reponse_data'][0]['key'];
$Response_GenderKey = $content['reponse_data'][1]['key'];
$Response_IncomeKey = $content['reponse_data'][2]['key'];
$Response_CityKey = $content['reponse_data'][3]['key'];
$Response_StateKey = $content['reponse_data'][4]['key'];
$Response_CountryKey = $content['reponse_data'][5]['key'];
$Response_Age = $content['reponse_data'][0]['value'];
$Response_Gender = $content['reponse_data'][1]['value'];
$Response_Income = $content['reponse_data'][2]['value'];
$Response_City = $content['reponse_data'][3]['value'];
$Response_State = $content['reponse_data'][4]['value'];
$Response_Country = $content['reponse_data'][5]['value']

`

2 Answers 2

1

the "response_data" is not valid JSON. There is a pair of extra double quotes enclosing the array. Remove the enclosing double quotes then it should work.

{
  "address": "4 Ficticious Ave",
  "city": "Miami",
  "country": "United States",
  "email": "[email protected]",
  "first_name": "Jane",
  "last_name": "Doe",
  "state": "FL",
  "zip_code": "03423",
  "response_data": [
    {
      "key": "7122",
      "value": "37-52"
    },
    {
      "key": "7123",
      "value": "Female"
    },
    {
      "key": "7124",
      "value": "$35,000 to $50,000 USD"
    },
    {
      "key": "6176",
      "value": "Miami"
    },
    {
      "key": "6177",
      "value": "FL"
    },
    {
      "key": "6179",
      "value": "United States"
    }
  ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this....it helped for me to have the removal code as provided by @Elementary.
0

As someone said the "response_data" is not valid JSON. There is a pair of extra double quotes enclosing the array.But i think if it is a response and not a manually typed json it will be better to automatically normalize the received string before decode it with Json_decode mainly by Removing the enclosing double .You can use the code below to achieve it:

<?php
 // Identify the content as json
header("Content-Type: application/json; charset=UTF-8");

// get the contents of the JSON file
$data = file_get_contents("php://input");

//normalize the json in order to be properly decoded

$start=strpos($data,':',strpos($data,'response_data'));
$get=substr($data,$start+1,strrpos($data,'"')-$start);
$data=str_replace($get,trim(trim($get),'"'),$data);

//decode JSON data to PHP array
$content = json_decode($data, true);

//Fetch the details of customer

$Cust_Fname = $content['first_name'];
$Cust_Lname = $content['last_name'];
$Cust_Email = $content['email'];
$Street_Address = $content['address'];
$City = $content['city'];
$State = $content['state'];
$Country = $content['country'];
$Postal_Code = $content['zip_code'];

//also fetch the appended "array" of key/value fields...
$Response_AgeKey = $content['reponse_data'][0]['key'];
$Response_GenderKey = $content['reponse_data'][1]['key'];
$Response_IncomeKey = $content['reponse_data'][2]['key'];
$Response_CityKey = $content['reponse_data'][3]['key'];
$Response_StateKey = $content['reponse_data'][4]['key'];
$Response_CountryKey = $content['reponse_data'][5]['key'];
$Response_Age = $content['reponse_data'][0]['value'];
$Response_Gender = $content['reponse_data'][1]['value'];
$Response_Income = $content['reponse_data'][2]['value'];
$Response_City = $content['reponse_data'][3]['value'];
$Response_State = $content['reponse_data'][4]['value'];
$Response_Country = $content['reponse_data'][5]['value']

?>

1 Comment

Thanks, it always helps to have an extra pair of eyes! Ok, so the code runs without error and the data gets inserted as a new record in MySQL [code not provided]. Strange thing is that none of the data from the response_data key value pairs shows up...all of those columns are blank. Guess I gotta play around with that retrieval syntax. Thanks much...I am much further along now.

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.