question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Comparing integers from JSON data

See original GitHub issue

Hello! I’m trying to compare two integers which I’m bringing in as JSON, i.e.:

var template = Template.ParseLiquid("{% if paginator.page != paginator.total_pages %}");
return template.Render(new { paginator = SomeJObject });

However, this yields the following error: <input>(47,24) : error : Unsupported types `1/Newtonsoft.Json.Linq.JValue` >= `3/Newtonsoft.Json.Linq.JValue` for binary operation

I first thought that this is due to the two integers being JValues, not technically ints, but if I replace one of them in the comparison with a hardcoded integer, it works, and the error does show their respective values (1 and 3, respectively), so I know Scriban knows what they are. I’d appreciate some guidance on this. Thank you.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Jul 25, 2020

For anyone else using Json.NET in the future, finding this page through Google, this is what I ended up with:

private static object ConvertFromJson(JObject element)
        {
            switch (element.Type)
            {
                case JTokenType.Object:
                    var obj = new ScriptObject();
                    foreach (var prop in element.Properties())
                    {
                        //Console.WriteLine(prop.Name + ": " + prop.Value.ToString());
                        obj[prop.Name] = prop.Value.ToObject<object>();
                    }

                    return obj;
                case JTokenType.Array:
                    var array = new ScriptArray();
                    foreach (var nestedElement in element.Descendants())
                    {
                        array.Add(ConvertFromJson(JObject.Parse(nestedElement.ToString())));
                    }
                    return array;
                case JTokenType.String:
                    return element.ToString();
                case JTokenType.Integer:
                    return element.ToObject<int>();
                case JTokenType.Float:
                    return element.ToObject<double>();
                case JTokenType.Boolean:
                    return element.ToObject<bool>();
                default:
                    return null;
            }
        }

However, this does not explain the fact that == worked earlier. It does fix my problem, though. Yay.

I’m not sure whether or not I handled arrays properly with that, so you may have to do some further fiddling about.

1reaction
xoofxcommented, Jul 25, 2020

Example of some code using System.Text.Json to convert to Scriban:

      private static object ConvertFromJson(JsonElement element)
        {
            switch (element.ValueKind)
            {
                case JsonValueKind.Object:
                    var obj = new ScriptObject();
                    foreach (var prop in element.EnumerateObject())
                    {
                        obj[prop.Name] = ConvertFromJson(prop.Value);
                    }

                    return obj;
                case JsonValueKind.Array:
                    var array = new ScriptArray();
                    foreach (var nestedElement in element.EnumerateArray())
                    {
                        array.Add(ConvertFromJson(nestedElement));
                    }
                    return array;
                case JsonValueKind.String:
                    return element.GetString();
                case JsonValueKind.Number:
                    if (element.TryGetInt32(out var intValue))
                    {
                        return intValue;
                    }
                    else if (element.TryGetInt64(out var longValue))
                    {
                        return longValue;
                    }
                    else if (element.TryGetUInt32(out var uintValue))
                    {
                        return uintValue;
                    }
                    else if (element.TryGetUInt64(out var ulongValue))
                    {
                        return ulongValue;
                    }
                    else if (element.TryGetDecimal(out var decimalValue))
                    {
                        return decimalValue;
                    }
                    else if (element.TryGetDouble(out var doubleValue))
                    {
                        return doubleValue;
                    }
                    else
                    {
                        throw new InvalidOperationException($"Unable to convert number {element}");
                    }
                case JsonValueKind.True:
                    return true;
                case JsonValueKind.False:
                    return false;
                default:
                    return null;
            }
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to compare integer field with json field?
1 Answer 1 ... This line if(bxqtyy.getText().toString() > minimum_qty.compareTo(BOX_QTY)) is not working because you can't compare string with ...
Read more >
What is the correct way to compare jsonPath's value as an ...
Hi, I would like to perform a simple check that verifies that the JSON response contains an integer that is greater than 0....
Read more >
Compare Two JSON Objects with Jackson
Learn how to use Jackson to compare two JSON objects using the built-in comparator and a custom comparator.
Read more >
nlohmann::basic_json::operator - JSON for Modern C++
Integer and floating-point numbers are automatically converted before comparison. Compares a JSON value and a scalar or a scalar and a JSON value...
Read more >
Compare JSON Objects with Jackson
Compare JSON Objects with Custom Comparator ... The JsonNode.equals() method works fine for most of the cases in comparing two objects. However, Jackson...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found