0

I have 2 different jsons and I need to get one inside the other.

JSON 1

[{
    "id":"1",
    "texto":"Vamos newells 17471934",
    "fecha":"2019-06-24 12:09:12",
    "categoria":"1",
    "idpayment":"Reference_1561388952",
    "provincia":"1",
    "estado":"NO",
    "email":"newells@gmail.com"
}]

JSON 2

{
    "Texto": " VENDO O PERMUTO",
    "imageJob": {
        "pathConvertido": "ClasificadosPNG/0011311247.png",
        "convertido": true,
        "id": 5011
    },
    "rubroClasificado": {
        "CodigoRubro": 150,
        "id": 76
    }
}

I need the second one inside the first one for use with javascript

I tried the array_merge() php function without getting results

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • What is your expected output? And in order to use `array_merge()`, you have to decode the JSON first. – Qirel Jun 26 '19 at 16:50
  • Do you want just second json inside first, or You want them in an array form? – sujeet Jun 26 '19 at 16:57
  • 1
    Possible duplicate of [Merging two json in PHP](https://stackoverflow.com/questions/20286208/merging-two-json-in-php) – Qirel Jun 26 '19 at 17:02
  • There is a great answer to this question in another question: https://stackoverflow.com/questions/20286208/merging-two-json-in-php $combinedJson = json_encode(array_merge(json_decode($a, true),json_decode($b, true))) – raw-bin hood Jun 26 '19 at 16:55

4 Answers4

0

You can use json_decode with parameter true to convert JSON into an array , then use array_merge to make them a single array

$j1 = '[{"id":"1","texto":"Vamos newells 17471934","fecha":"2019-06-24 12:09:12","categoria":"1","idpayment":"Reference_1561388952","provincia":"1","estado":"NO","email":"newells@gmail.com"}]';
$j1arr = json_decode($j1, true);
$j2 = '  {
    "Texto": " VENDO O PERMUTO",
    "imageJob": {
        "pathConvertido": "ClasificadosPNG/0011311247.png",
        "convertido": true,
        "id": 5011
    },
    "rubroClasificado": {
        "CodigoRubro": 150,
        "id": 76
    }
}';
$j2arr = json_decode($j2, true);
$r = array_merge($j1arr[0], $j2arr);

You can use the merged array in javascript by using json_encode

json_encode($r)

https://3v4l.org/WKX1U

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

Do it simply like this way without any extra array merging methods-

<?php
$json1 = json_decode('[{"id":"1","texto":"Vamos newells 17471934","fecha":"2019-06-24 12:09:12","categoria":"1","idpayment":"Reference_1561388952","provincia":"1","estado":"NO","email":"newells@gmail.com"}]',1);
$json2 = json_decode('{"Texto":" VENDO O PERMUTO","imageJob":{"pathConvertido":"ClasificadosPNG/0011311247.png","convertido":true,"id":5011},"rubroClasificado":{"CodigoRubro":150,"id":76}}',1);
$json1[] = $json2; // add second json to first json
print_r($json1);
echo json_encode($json1);
?>

WORKING DEMO: https://3v4l.org/qhsJq

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • 1
    Second json inside first json. While it will give array of two associative arrays. – sujeet Jun 26 '19 at 17:01
  • @SujeetAgrahari then may be I misunderstood the question, I though OP want's to append the second one to first one. May be he needs it on same array level like first one with `array_merge()` – A l w a y s S u n n y Jun 26 '19 at 17:04
0

As you said you want to process the resulting data in JavaScript, and as per your question you want second json inside the first json. You can do something like this. You will get a json as final result.

$first = '[{
"id":"1",
"texto":"Vamos newells 17471934",
"fecha":"2019-06-24 12:09:12",
"categoria":"1",
"idpayment":"Reference_1561388952",
"provincia":"1",
"estado":"NO",
"email":"newells@gmail.com"
}]';

$first = str_replace(['[',']','}'], '', $first);

$second = '{
"Texto": " VENDO O PERMUTO",
"imageJob": {
    "pathConvertido": "ClasificadosPNG/0011311247.png",
    "convertido": true,
    "id": 5011
},
"rubroClasificado": {
    "CodigoRubro": 150,
    "id": 76
 }
}';

$second = preg_replace('/\{/', ',', $second,1);


print_r($first.$second);

As result you will get a valid json, second json inside your first json, you can validate here

sujeet
  • 3,480
  • 3
  • 28
  • 60
0

since JSON1 is an array of JSON-object and JSON2 is just a JSON-object.

in addition to that you said, 'I need the second one inside the first one for use with javascript'. therefore, you can test this code https://3v4l.org/1HGNF/perf#output

please compare all performances in mem-usage and timing

$j1 = '[{
    "id":"1",
    "texto":"Vamos newells 17471934",
    "fecha":"2019-06-24 12:09:12",
    "categoria":"1",
    "idpayment":"Reference_1561388952",
    "provincia":"1",
    "estado":"NO",
    "email":"newells@gmail.com"
}]';
$j2 = '{
    "Texto": " VENDO O PERMUTO",
    "imageJob": {
        "pathConvertido": "ClasificadosPNG/0011311247.png",
        "convertido": true,
        "id": 5011
    },
    "rubroClasificado": {
        "CodigoRubro": 150,
        "id": 76
    }
}';
$j1 = json_decode($j1, true);
$j2 = json_decode($j2, true);
$j1[] = $j2;
var_dump(json_encode($j1));
m3hr24d
  • 111
  • 5