The PHM
The PHM

Reputation: 11

PHP json_encoded arrays fail mongoimport

I have a PHP script that dumps a list of people using json_decode()

foreach ( $personArray as $person ) {               
    $currentPosition = $person->loadCurrentPosition();
    $one[ FIELD_NAME ] = $person->getName();
    $one[ FIELD_ID ] = $person->getId();
    $one[ FIELD_ORGANIZATION_ID ] = $currentPosition->organization->getId();
    $one[ FIELD_ORGANIZATION_NAME ] = $currentPosition->organization->getName();
    $result[] = $one;
}
$jsonResult = json_encode( $result  );

I put the result into a file called 'people.json' :

[
    {
        "name": "Bobby Brown",
        "id": 32632,
        "organizationid": 40492,
        "organizationname": "Exinda Networks"
    },
    {
        "name": "Billy Bob",
        "id": 32633,
        "organizationid": 29824,
        "organizationname": "Desire2Learn"
    }
]

I used mongoimport (mongodb v3.0.7):

mongoimport --db test --collection people --drop --file people.json

I get the following error and 0 documents imported:

2015-11-07T17:02:35.461-0500    Failed: error unmarshaling bytes on document #0: JSON decoder out of sync - data changing underfoot?

I remove the leading square bracket '[' and trailing ']' in the file and try mongoimport again:

2015-11-07T17:13:37.575-0500    Failed: error processing document #2: invalid character ',' looking for beginning of value

I remove the ',' between the two JSON objects in the file and the import works:

2015-11-07T17:16:12.162-0500    imported 2 documents

Is json_encode() incorrectly encoding?

Will one of the json_encode() options result in a format that mongoimport likes?

Upvotes: 0

Views: 278

Answers (1)

The PHM
The PHM

Reputation: 11

add "--jsonArray" option to mongoimport

big thanks to @amber4478 who answered this question Mongoimport of json file

Upvotes: 1

Related Questions