Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentError: invalid JSON at byte position 11 while parsing type Int64: InvalidChar? #74

Closed
MrDataPsycho opened this issue Sep 19, 2020 · 1 comment

Comments

@MrDataPsycho
Copy link

Hi,
Suppose I have two Structs one for Tweet and another for Author of the tweet as follows:

struct User
    id::Union{Int64, Nothing}
    id_str::Union{String, Nothing}
    created_at::Union{String, Nothing}
    name::Union{String, Nothing}
    screen_name::Union{String, Nothing}
    location::Union{String, Nothing}
    statuses_count::Union{Int64, Nothing}
    followers_count::Union{Int64, Nothing}
    description::Union{String, Nothing}
    profile_image_url::Union{String, Nothing}
end

struct Tweet
    id::Union{Int64, Nothing}
    id_str::Union{String, Nothing}
    created_at::Union{String, Nothing}
    favorite_count::Union{Int, Nothing}
    retweet_count::Union{Int, Nothing}
    full_text::Union{String, Nothing}
    lang::Union{String, Nothing}
    place::Union{String, Nothing}
    truncated::Union{Bool, Nothing}
    user::User
end

Now I have a Json String as :

json_str = "{\"id_str\":\"1305501948074835974\",\"created_at\":\"Mon Sep 14 13:41:34 +0000 2020\",\"place\":null,\"id\":1305501948074835974,\"user\":{\"name\":\"Donald J. Trump\",\"id_str\":\"25073877\",\"created_at\":\"Wed Mar 18 13:46:38 +0000 2009\",\"id\":25073877}}"

I am trying to define a New Struct function and read the string in to value of type:

JSON3.StructType(::Type{Tweet}) = JSON3.Struct()
JSON3.read(json_str, Tweet)

I am getting the following error:

ArgumentError: invalid JSON at byte position 11 while parsing type Int64: InvalidChar
{"id_str":"1305501948074835974","cre

Does the string need to match the exact order of the Struct? or Not sure where is the Issue. Though the JSON3.read(json_str) works fine.

If someone can tell me:

  • How to read such data in to some value of Given struct type (Tweet)
  • Is it possible to convert the value to Dictionary using Json3
@quinnj
Copy link
Owner

quinnj commented Sep 20, 2020

Yes, in the docs for Struct, it mentions that fields need to be explicitly ordered; currently, to parse unordered fields, you need to make your type mutable struct with a Mutable() struct type. This may change in the future since I (and others) have been discussing solutions in #47

@quinnj quinnj closed this as completed Sep 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants