Skip to content

fromJSON() with nested array #436

@berthetclement

Description

@berthetclement

Hello,

I would like to read the content of a JSON file that is formatted either in "array" mode ([{"id": "79f4b5801", "owner": {"id": 251, "name": "titi toto"}}]') or in JSON mode without the "[]" ('{"id": "79f4b5801", "owner": {"id": 251, "name": "titi toto"}}').

With the square brackets, the result of the following code is ok :

good <- fromJSON('[{"id": "79f4b5801", "owner": {"id": 251, "name": "titi toto"}}]')
as.data.table(good)

id owner.id owner.name
1: 79f4b5801 251 titi toto

Now without square brackets :

bad <- fromJSON('{"id": "79f4b5801", "owner": {"id": 251,"name": "titi toto"}}')
as.data.table(bad)

id owner
1: 79f4b5801 251
2: 79f4b5801 titi toto

I don't have a solution for getting the right result without the square brackets, and for the moment I'm forced to use the paste0() to add square brackets.

it seems trivial but I haven't found, with the fromJSON() parameters, how to format the result correctly.

Could you give me an idea?

Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions