How can I do this change in json? [duplicate] - php

This question already has answers here:
Pretty-Printing JSON with PHP
(27 answers)
Closed 1 year ago.
Hi I have php code which I use json_encode to create from an array a json code, the thing or porblem is that json_encode becomes the array to a lineal json what I mean it this:
$data["auth"]["cert"] = ["-----BEGIN CERTIFICATE-----\nMIIGjjCCBXagAwIBAgIDAR4jMA0GCSqGSIb3DQEBCwUAMIGmMQswCQYDVQQGEwJD\nTDEYMBYGA1UEChMPQWNlcHRhLmNvbSBTLkEuMUgwRgYDVQQDEz9BY2VwdGEuY29t\nIEF1dG9yaWRhZCBDZXJ0aWZpY2Fkb3JhIENsYXNlIDMgUGVyc29uYSBOYXR1cmFs\nIC0gRzQxHjAcBgkqhkiG9w0BCQEWD2luZm9AYWNlcHRhLmNvbTETMBEGA1UEBRMK\nOTY5MTkwNTAtODAeFw0yMTA3MDgyMzQwMjZaFw0yNDA3MDgyMzQwMjZaMIGXMQsw\nCQYDVQQGEwJDTDEYMBYGA1UEDBMPUEVSU09OQSBOQVRVUkFMMSswKQYDVQQDEyJN\nQVJDRUxPIEFMRUpBTkRSTyBJTlpVTlpBIEdPTlpBTEVssaMSwwKgYJKoZIhvcNAQkB\nFh1DUklTVElBTklOWlVOWkFASklTUEFSS0lORy5DTDETMBEGA1UEBRMKMTAwMzM3\nNDEtSzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK/zoDtqd7+qB7aX\njS/kiBnLOf62orrMMdL3G22l0NJBziWVv/DezIgoFONUEF2XNXBWNAKTbLABvGIZ\nNFLyE4QuevdLd5AXuOt0q4+Y2msZemrP3zIkuT1dkNo/djj5gmFY3hC1i9DEj2ZQ\nF3kwcN9rrhQrzg3I0ixrRfdozMIypvCY4pWoJOvaNc8Z1a2Brq09QXQ3zUB3jXW7\nezxQlOCqv6G37UB9FzInnwAmicz5/92YtvFAXXJd4dtuaoPQt7Hn8XzhP8wmtj9j\nE9uHUbzK4CmQm4bYb1hmceg4crTHybTLUHngSlfI7QbDsLroaUlNgq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3CNPsNFw/vCvqrb839ZUrBCh+IqCMrU/nrLuqmCpBIG+2/\naiNHgE7pIhfF/QpCbDwlbIH0HxQUTdPnJvAmmwZWGIkCgYEA/xF0WM4pQCRQF+Og\nCNdKjWkF/ZXS/tnpjxr2DmvMKwZX2nXMBHC7aHpfUCzj/Td69uK+AG4LnRkP1L3U\nyV0kXbMGUFcofVhYTDnPPNdlNyL5n2LKEmnYAYaJyYitEGoJN2hwplDMGH8kSKBH\nuymYN5ry5FfQfJGh17cjb/vP1s8CgYEAsJguDDB0uNC/oCEi1tSDG4RopV8tk/PH\nbtK1Fk0Qu7NliHr+PKb9w69wiw8BZ7dDU+igqJrywXl+Tnm8CvpbOAL8qQZqMzXM\nGvGBelWSq2HzxrIURD4X1yqzPvRKCTEbAPn4cgnmUBIpOtkdDJjrwuMD8dIT6J4y\nqmlpYZEWYwMCgYAEw4awwejzUbpNN+sdPygdTADYo5u1Nsyt54sA6fJ+OzgY1Gpj\nCtf1M5PkI3J+oDKjuchiqat926H4DzOSLzMmrNlJVtdiv+umQM4mDL/PL9AJsgak\nIWXvYVvhb7QLwm85obG46XlmW7mJwbSVQkmdgD9ZFGrIaM/k/36h8MoI8QKBgD8R\nChjmUTkTq+vXCacpW+0+21R76j4VaJrmey+MtDYkelVEf3lPtf7lr86pvDm7FDtq\nL74nIB0Cc545EXPmNx+IyYzfspu5UbwplbEH0IqOP84tGNnKRx9bq4oHGk2wENHH\nc/feGzdrVPgkQ6CVGFWQV39MJDoGDVgYrz7d3t3bAoGBAK+UnIFrRJ225UMsIM5Ao2\nryKtwPemdHypIVK3WOV1yRpa5aBqemrTQkijbLx8lVxoeID8Lw4zHybK6V9mAHsf\nRhfHsmBz3f4mYoKVoyVDelxpx+IGkGibvzNQB5BFWuu8kZeDPkhcPbk4pHFUf/jm\nCvLdtcSmumV5hxnVdtwjdlQD\n-----END PRIVATE KEY-----"];
$data["emisor"] = "76063822-6";
$data["resolucion"] = ["fecha" => "2014-06-17", "numero" => 57];
$data["secuencia"] = 1;
$data["documentos"][] = [
"TpoDoc" => 39,
"NroDoc" => 3,
"TasaImp" => 0,
"FchDoc" => "2021-07-05",
"MntExe" => 100,
"MntNeto" => 0,
"MntIVA" => 0,
"MntTotal" => 100
];
$data = json_encode($data);
That array becomes to this:
{"auth":{"cert":{"cert-data":"-----BEGIN CERTIFICATE-----\nMIIGjjCCBXagAwIBAgIDAR4jMA0GCSqGSIb3DQEBCwUAMIGmMQswCQYDVQQGEwJD\nTDEYMBYGA1UEChMPQWNlcHRhLmNvbSBTLkEuMUgwRgYDVQQDEz9BY2VwdGEuY29t\nIEF1dG9yaWRhZCBDZXJ0aWZpY2Fkb3JhIENsYXNlIDMgUGVyc29uYSBOYXR1cmFs\nIC0gRzQxHjAcBgkqhkiG9w0BCQEWD2luZm9AYWNlcHRhLmNvbTETMBEGA1UEBRMK\nOTY5MTkwNTAtODAeFw0yMTA3MDgyMzQwMjZaFw0yNDA3MDgyMzQwMjZaMIGXMQsw\nCQYDVQQGEwJDTDEYMBYGA1UEDBMPUEVSU09OQSBOQVRVUkFMMSswKQYDVQQDEyJN\nQVJDRUxPIEFMRUpBTkRSTyBJTlpVTlpBIEdPTlpBTEVaMSwwKgYJKoZIhvcNAQkB\nFh1DUklTVElBTklOWlVOWkFASklTUEFSS0lORy5DTDETMBEGA1UEBRMKMTAwMzM3\nNDEtSzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK/zoDtqd7+qB7aX\njS/kiBnLOf62orrMMdL3G22l0NJBziWVv/DezIgoFONUEF2XNXBWNAKTbLABvGIZ\nNFLyE4QuevdLd5AXuOt0q4+Y2msZemrP3zIkuT1dkNo/djj5gmFY3hC1i9DEj2ZQ\nF3kwcN9rrhQrzg3I0ixrRfdozMIypvCY4pWoJOvaNc8Z1a2Brq09QXQ3zUB3jXW7\nezxQlOCqv6G37UB9FzInnwAmicz5/92YtvFAXXJd4dtuaoPQt7Hn8XzhP8wmtj9j\nE9uHUbzK4CmQm4bY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echa":"2014-06-17","numero":57},"secuencia":1,"documentos":[{"TpoDoc":39,"NroDoc":3,"TasaImp":0,"FchDoc":"2021-07-05","MntExe":100,"MntNeto":0,"MntIVA":0,"MntTotal":100}]}
I mean in a json of one line BUT I need this kind of json:
{
"auth":{
"cert":{
"cert-data":"-",
"pkey-data":""
}
},
"emisor":"76063822-6",
"resolucion":{
"fecha":"2014-06-17",
"numero":57
},
"secuencia":1,
"documentos":[
{
"TpoDoc":39,
"NroDoc":3,
"TasaImp":0,
"FchDoc":"2021-07-05",
"MntExe":100,
"MntNeto":0,
"MntIVA":0,
"MntTotal":100
}
]
}
How you can see this json is not in one line I wonder how can I transform that lineal json to this kind of json if I am using json_encode?
Thanks

Just format it this way:
$data = json_encode($data, JSON_PRETTY_PRINT);
More info: https://www.php.net/manual/en/function.json-encode.php

Related

Is there a way how to "encode" PHP array into PHP code? [duplicate]

This question already has answers here:
print an array as code
(4 answers)
Closed 2 years ago.
for very special purpose I need to transform PHP array into PHP code. I will try to explain what I mean in code below:
$a = array('a' => 'abc', 'path' => INCLUDE_DIR.'/file.txt', 'number' => 1234);
$php_code = some_magic_function($a);
echo $php_code; // outputs: "array('a' => 'abc', 'path' => '/path/to/file.txt', 'number' => 1234)"
So it would write back array as PHP code. I can not find any library that would help me with this.
Any ideas or hints ? Thanks in advance.
I think you want to print the array as a string, in the exact format it would be written in your code.
If so, then the var_export() function is what you need, e.g. something like this:
$a = array('a' => 'abc', 'path' => '/file.txt', 'number' => 1234);
$str = var_export($a, true);
echo $str;
Demo: http://sandbox.onlinephpfunctions.com/code/0b1da8fc0199a34539a55313680e982f9fddd14f

How to add spaces and new lines when using json_encode()? [duplicate]

This question already has answers here:
Pretty-Printing JSON with PHP
(27 answers)
Closed 3 years ago.
I've seen multiple answers in here but none of them had a solution to my question, so I made an account to ask this question. I understand now that \n is not an allowed character in json because the backslash is not allowed and that's why the problem is occurring.
I have the following code to encode an array in json:
<?php
$data = array('test1' => 'something1', 'test2' => 'something2', 'test3' => 'something3');
echo json_encode($data);
I'm trying to have the string outputted as follows:
{
"test1": "something1",
"test2": "something2",
"test3": "something3"
}
But what I'm getting is this:
{"test1":"something1","test2":"something2","test3":"something3"}
This is my go at it:
<?php
$data = array('test1' => 'something1\n', 'test2' => 'something2\n', 'test3' => 'something3\n');
echo json_encode($data);
but this returns
{"test1":"something1\n","test2":"something2\n","test3":"something3\n"}
You can add a second parameter to json_encode. This is called the JSON_PRETTY_PRINT constant:
<?php
$data = array('test1' => 'something1', 'test2' => 'something2', 'test3' => 'something3');
echo json_encode($data, JSON_PRETTY_PRINT);
You can use JSON_PRETTY_PRINT with header
$data = array('test1' => 'something1', 'test2' => 'something2', 'test3' => 'something3');
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data,JSON_PRETTY_PRINT);
OR
$json= json_encode($data,JSON_PRETTY_PRINT);
printf("<pre>%s</pre>", $json);
JSON is an interchange format, so you need not to be concerned about the looks very much.
Anyway, you can use flag constants to alter the encoding process
<?php
$a = '{"test1":"something1","test2":"something2","test3":"something3"}';
print_r(json_encode(json_decode($a, true), JSON_PRETTY_PRINT));
See live example: https://3v4l.org/CEc3L

Issues in json_encode [duplicate]

This question already has answers here:
Best way to create an empty object in JSON with PHP?
(4 answers)
Closed 6 years ago.
I have this array:
$array = [
"stored_fields" => ["id"],
"match_all" => []
];
I want:
{"stored_fields":["id"],"match_all":{}}
But, When I call:
echo json_encode($array);
I got:
{"stored_fields":["id"],"match_all":[]}
If I use:
echo json_encode($array, JSON_FORCE_OBJECT);
I got:
{"stored_fields":{"0":"id"},"match_all":{}}
What I can do?
There might be a better way, but you can just force an object:
$array = [
"stored_fields" => ["id"],
"match_all" => (object)[]
];
You could also use (object)null. Anything else will result in {"scalar":someting}. Another option is new stdClass.

How to convert json objects into a PHP array [duplicate]

This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 8 years ago.
I want to be able to convert my JSON object into an array is there a built in function in php that allows me to do this?
Here is my json object:
in_userid: "38"
in_email: "user#email.com"
in_firstname: "John"
in_lastname: "Mason"
in_mobilenumber: "00000000000"
in_password: "pass12345"
in_phonenumber: "0000000"
and I want it to look something like this:
Array(
in_userid => "38",
in_email => "user#email.com",
in_firstname => "John",
in_lastname => "Mason",
in_mobilenumber => "00000000000",
in_password => "pass12345",
in_phonenumber => "0000000",
);
How will I do this?
use json_decode(). It takes a JSON encoded string and converts it into a PHP variable. See the json_decode function in the docs.

Convert a multidimensional array to an XML object in PHP [duplicate]

This question already has answers here:
How to convert array to SimpleXML
(34 answers)
Closed 8 years ago.
I have a multidimensional array like this:
$array = array(
"hello" => "hola",
"another_array" => array(
"key" => "best key ever",
"another" => "yes, another key",
),
"coolarray" => array(
"bool" => true,
"string" => "this is a string!",
),
);
I want a class like this:
class MyClass {
public $array;
public function __construct($array) {
// something
$this->array_to_xml($array);
}
public function array_to_xml($array) {
// convert array to xml
}
Then I want to be able to do things like this:
$string = $this->array->coolarray->string;
How can I do that?
This gets asked a lot
Not sure why you mention XML, sounds like you just want an object.
See this answer for example:
https://stackoverflow.com/a/11854285/543455
$obj = json_decode(json_encode($array));

Categories