How to convert a JSON string to an array

I want to do the following:

  • taking JSON as input from text area in php
  • use this input and convert it to JSON and pass it to php curl to send the request.

this m get at php from get of api this json string, which I want to pass to json, but it does not convert to an array

echo $str='{ action : "create", record: { type: "n$product", fields: { n$name: "Bread", n$price: 2.11 }, namespaces: { "my.demo": "n" } } }'; $json = json_decode($str, true); 

the above code does not return me an array.

+75
json arrays php
Sep 22 '11 at 8:27
source share
11 answers

If you pass JSON in your message to json_decode , it will fail. Valid JSON strings have quoted keys:

 json_decode('{foo:"bar"}'); // this fails json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar") json_decode('{"foo":"bar"}'); // returns an object, not an array. 
+104
Sep 22 2018-11-11T00:
source share

Try the following:

 $data = json_decode($your_json_string, TRUE); 

the second parameter will make the decoded json string into associative arrays.

+52
Sep 22 '11 at 8:31
source share

If you get a JSON string from a form using $_REQUEST , $_GET or $_POST , you will need to use the html_entity_decode() function. I did not understand this until I did var_dump what was in the request, and what I copied, and the echo operator, and noticed that the query line was much larger.

The right way:

 $jsonText = $_REQUEST['myJSON']; $decodedText = html_entity_decode($jsonText); $myArray = json_decode($decodedText, true); 

With mistakes:

 $jsonText = $_REQUEST['myJSON']; $myArray = json_decode($jsonText, true); echo json_last_error(); //Returns 4 - Syntax error; 
+16
Jan 28 '13 at 4:42
source share

Use the json_decode($json_string, TRUE) function to convert a JSON object to an array.

Example:

 $json_string = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; $my_array_data = json_decode($json_string, TRUE); 

NOTE. The second parameter converts the decoded JSON string to an associative array.

============

Output:

 var_dump($my_array_data); array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } 
+9
Jul 11 '15 at 9:32
source share

If you get the json string from the URL using file_get_contents , follow these steps:

 $url = "http://localhost/rest/users"; //The url from where you are getting the contents $response = (file_get_contents($url)); //Converting in json string $n = strpos($response, "["); $response = substr_replace($response,"",0,$n+1); $response = substr_replace($response, "" , -1,1); print_r(json_decode($response,true)); 
+4
07 Sep
source share

your string should be in the following format:

 $str = '{"action": "create","record": {"type": "n$product","fields": {"n$name": "Bread","n$price": 2.11},"namespaces": { "my.demo": "n" }}}'; $array = json_decode($str, true); echo "<pre>"; print_r($array); 

Output:

 Array ( [action] => create [record] => Array ( [type] => n$product [fields] => Array ( [n$name] => Bread [n$price] => 2.11 ) [namespaces] => Array ( [my.demo] => n ) ) ) 
+3
Oct 08 '15 at 18:22
source share

If you ever need to convert a JSON file or structures into arrays in the style of PHP, with all levels of nesting, you can use this function. First, you must json_decode ($ yourJSONdata), and then pass it to this function. It will display the correct PHP-style arrays in your browser window (or console).

https://github.com/mobsted/jsontophparray

+1
Mar 03 '16 at 15:13
source share
 <?php $str='{ "action" : "create", "record" : { "type": "$product", "fields": { "name": "Bread", "price": "2.11" }, "namespaces": { "my.demo": "n" } } }'; echo $str; echo "<br>"; $jsonstr = json_decode($str, true); print_r($jsonstr); ?> 

I think this should work, just so that the keys are also in double quotes if they are not numbers.

+1
Oct 30 '17 at 11:32
source share

if you want to convert your array to json and vice versa, you can try http://framework.zend.com/manual/en/zend.json.basics.html
just use the zend_json class, it has many nice features.

0
Sep 22 '11 at 8:32
source share

Use this converter, it does not fail: Services_Json

 // create a new instance of Services_JSON $json = new Services_JSON(); // convert a complexe value to JSON notation, and send it to the browser $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4))); $output = $json->encode($value); print($output); // prints: ["foo","bar",[1,2,"baz"],[3,[4]]] // accept incoming POST data, assumed to be in JSON notation $input = file_get_contents('php://input', 1000000); $value = $json->decode($input); // if you want to convert json to php arrays: $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); 
0
Sep 05 '17 at 12:00
source share

this is my solution: json string $columns_validation = string(1736) "[{"colId":"N_ni","hide":true,"aggFunc":null,"width":136,"pivotIndex":null,"pinned":null,"rowGroupIndex":null},{"colId":"J_2_fait","hide":true,"aggFunc":null,"width":67,"pivotIndex":null,"pinned":null,"rowGroupIndex":null}]"

so I use json_decode twice:

 $js_column_validation = json_decode($columns_validation); $js_column_validation = json_decode($js_column_validation); var_dump($js_column_validation); 

and the result:

  array(15) { [0]=> object(stdClass)#23 (7) { ["colId"]=> string(4) "N_ni" ["hide"]=> bool(true) ["aggFunc"]=> NULL ["width"]=> int(136) ["pivotIndex"]=> NULL ["pinned"]=> NULL ["rowGroupIndex"]=> NULL } [1]=> object(stdClass)#2130 (7) { ["colId"]=> string(8) "J_2_fait" ["hide"]=> bool(true) ["aggFunc"]=> NULL ["width"]=> int(67) ["pivotIndex"]=> NULL ["pinned"]=> NULL ["rowGroupIndex"]=> NULL } 
0
Dec 04 '17 at 19:32
source share



All Articles