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

Using "_xpath" on table with one row does not create an JSON array #25

Closed
FrederickBrier opened this issue May 8, 2019 · 4 comments
Closed

Comments

@FrederickBrier
Copy link

The _xpath feature seems to behave differently if there is only one result from the _xpath. It will generate:

"rows": { "col1": "val1", "col2": "val2" }

Instead of:

"rows": [{ "col1": "val1", "col2": "val2" }]

This makes it difficult to iterate over the array of rows, as you end up iterating over the columns. Thoughts? Is there a way to force it to be an array regardless of there being a single row? Thank you.

@PoLaKoSz
Copy link
Contributor

PoLaKoSz commented Dec 12, 2019

This is the first day i use this library but i don't think there is a nice way to solve our problem. (#25 (comment))
I came up with this simple fix (just wrap the JObject with a JArray ):

// ... 
JContainer scrapingResults = openScraping.Extract(html);
if (scrapingResults["row"].GetType() == typeof(JObject))
{
    JArray container = new JArray
    {
        scrapingResults["row"]
    };
    scrapingResults["row"] = container;
}
Console.WriteLine(JsonConvert.SerializeObject(scrapingResults, Formatting.Indented));

@zmarty
Copy link
Contributor

zmarty commented Dec 12, 2019

Did you try to use _forceArray option?

@zmarty
Copy link
Contributor

zmarty commented Dec 12, 2019

Something like this:

"title":
{
    "_xpath": "//h1",
    "_forceArray": true
}

@PoLaKoSz
Copy link
Contributor

PoLaKoSz commented Dec 12, 2019

Thank You! It works like a charm. 😄

@zmarty zmarty closed this as completed May 22, 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

3 participants