Question: I'm trying to use get the response headers using PHP.

Here's my current code:

```
foreach ($headers as $name => $value) {
  foreach ($value as $key => $moreinfo) {
    echo "$name === $moreinfo \n";
  }
}

```

I basically just don't want there to be multiple `$name` entries.

For example, I'm currently seeing this being echoed:

```
date === Sun, 03 Nov 2019 03:03:11 GMT 
date === Sun, 03 Nov 2019 03:03:11 GMT 
via === 1.1 varnish 
via === 1.1 varnish 
connection === keep-alive 
x-served-by === cache-bur17530-BUR 
x-served-by === cache-bur17548-BUR 

```

I would want it changed to just showing this:

```
date === Sun, 03 Nov 2019 03:03:11 GMT 
via === 1.1 varnish 
connection === keep-alive 
x-served-by === cache-bur17548-BUR 

```

Is there anything that could just be modified in the `foreach` to make this happen?

Answer: 
The JSON parsers only read the first root value of the input.

E.g. with the following JSON text, only the first object with field `foo` will be parsed, and then parsing stops, so it doesn't see the rest, which can then be anything.

```json
{
  "foo": 42
}
{
  "bar": 666
}

```

To validate the JSON text, and to verify that no extra content is present, you can use the underlying Jackson parser, instead of the high-level `ObjectMapper`.

```
public static void validateJSON(String json) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    try (JsonParser jsonParser = jsonFactory.createParser(json)) {
        if (jsonParser.nextToken() == null)
            throw new IOException("No JSON root element");
        if (jsonParser.skipChildren().nextToken() != null) {
            JsonLocation loc = jsonParser.getTokenLocation();
            throw new IOException("Multiple JSON root elements" +
                                  " at line " + loc.getLineNr() +
                                  ", column " + loc.getColumnNr());
        }
    }
}

```

*Test*

```
public static void test(String json) {
    try {
        validateJSON(json);
        System.out.println("Valid");
    } catch (Exception e) {
        System.out.println("Invalid: " + e);
    }
}

```

```
test("");
test("{}");
test("{ \"foo\": 42 }");
test("{\n" +
     "  \"foo\": 42\n" +
     "}\n" +
     "{\n" +
     "  \"bar\": 666\n" +
     "}");
test(Files.readString(Paths.get("test.json")));

```

The last is reading a file with the JSON from the question.

*Output*

```none
Invalid: java.io.IOException: No JSON root element
Valid
Valid
Invalid: java.io.IOException: Multiple JSON root elements at line 4, column 1
Invalid: java.io.IOException: Multiple JSON root elements at line 29, column 4

```
When you say that all the libraries that you've tested (jackson, gson, etc.) have the same result, what exactly do you mean? The same error message, the same exception class, the same stacktrace? Please post the result of the check in the question so that our colleagues could provide a better answer.

When you call the sites you've mentioned in the question, no-one guarantees that in the backend your sample json gets processed by one of the libraries you've tested, no-one guarantees even that the backend of the site is written in Java / any JVM language so that potentially these json libraries could not be even accessible for the developers of those sites.

One way or another, every library java based or not has its own algorithm of parsing the json object, so the errors can also be slightly different. That's the reason that you have different results, and its OK.