0

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?

2
  • use jsonlint.com to check the validity of your json. Commented Nov 7, 2015 at 22:52
  • thanks @aldrin27 for the PHP json_encode() file jsonlint.com says "Valid JSON". For the modified one that mongoimport read OK jsonlint.com says "Parse error" so it appears it's the mongoimport Commented Nov 8, 2015 at 0:45

1 Answer 1

1

add "--jsonArray" option to mongoimport

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

Sign up to request clarification or add additional context in comments.

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.