2

I have a json file which has series of json arrays

["FIRST_COLUMN", "SECOND_COLUMN", "THIRD_COLUMN" ...]
["John", "Snow", "Game of Thrones", ...]
["Ned", "Snow", "Game of Thrones", ....]
...

but I want one single json object:

[
  {"FIRST_COLUMN" : "JOHN", "SECOND_COLUMN" : "SNOW"... } ,
  {"FIRST_COLUMN" : "Ned", "SECOND_COLUMN" : "SNOW"... } ,
]

I want to do this in PHP and when I use json_encode I get a json but now in the same format, is there a built in function to do this? if not, how can I get the output ?

4
  • Decode it, loop it, build your new object, encode your new object, and call it a day. Commented Apr 30, 2018 at 17:00
  • 1
    The file you present is not JSON, it rather looks as if each line contained a piece of JSON. Knowing that, you should be able to read it, convert its representation and write it again. Commented Apr 30, 2018 at 17:00
  • so use json_encode first and then decode it again? Commented Apr 30, 2018 at 17:01
  • I don't understand what you are referring to with that follow-up question, @overflower. In any case, one thing to do is to try things before asking. People here have little respect for others that want to be spoon-fed solutions to their sometimes trivial homework problems. Commented Apr 30, 2018 at 17:02

2 Answers 2

2

You can do something like:

$str = '[["FIRST_COLUMN", "SECOND_COLUMN", "THIRD_COLUMN"],["John", "Snow", "Game of Thrones"],["Ned", "Snow", "Game of Thrones"]]';

//Convert string to array
$arr = json_decode($str, true);

//Remove the first array and store it in a variable
$header = array_shift($arr);

//Loop thru the remaining array
$results = array_map(function ($n) use($header) {
    return array_combine($header,$n); //Combine the arrays
}, $arr );

//Convert array to string
echo json_encode($results); 

This will result to:

[  
   {  
      "FIRST_COLUMN":"John",
      "SECOND_COLUMN":"Snow",
      "THIRD_COLUMN":"Game of Thrones"
   },
   {  
      "FIRST_COLUMN":"Ned",
      "SECOND_COLUMN":"Snow",
      "THIRD_COLUMN":"Game of Thrones"
   }
]

If your original value is string and not a valid json, you can:

$str = '
["FIRST_COLUMN", "SECOND_COLUMN", "THIRD_COLUMN"]
["John", "Snow", "Game of Thrones"]
["Ned", "Snow", "Game of Thrones"]
';

//Convert string to array | Explode by new line and filter.
$arr = array_filter(explode(PHP_EOL, $str),function($e){
    return trim($e) !== '';
});

//Remove the first array and store it in a variable
$header = json_decode(array_shift($arr), true);

$results = array_map(function ($n) use($header) {
    return array_combine($header,json_decode($n, true)); //Combine the arrays
}, $arr );

echo json_encode($results);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I noticed you modified the input, I did a preg_replace to convert the newline to comma, and added the brackets, if the data grows larger, preg_replace will be slow right?
Ohh your input is not a valid json? I would rather fix on how you put the data there instead of adding preg_replace
0

You need array_combine, that's what you need:

$keys = ['firstName', 'lastName', 'age'];
$values = ['John', 'Snow', 27];
$myEntry = array_combine($keys, $values);
// --> ['firstName' => 'John', 'lastName' => 'Snow', 'age' => 27]

just json_decode, loop through the entries, append the keys, json_encode

Comments

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.