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.

Please add a Skylark builtin for parsing JSON to a dict (useful for repo rules)

See original GitHub issue

When writing repository rules that inspect the state of the local system, I commonly want to run some local command and inspect its machine-readable output. The rule would then make configuration decisions based on available features, versions, installed plugins, and so on.

Nearly all tools that offer machine-readable version data use JSON, or at least offer it as an option. Rather than try to hack my way through with .split and .index, it would be much nicer if I could take a JSON string and have Bazel parse it directly into a dict within Skylark. A builtin json_to_dict(str) -> dict function would do nicely.

Using cmake as an example, I want to get its version using repository_ctx.execute():

$ cmake -E capabilities
{"version":{"isDirty":false,"major":3,"minor":9,"patch":1,"string":"3.9.1","suffix":""}}

… and then use that (3,9,1) tuple to decide whether it’s recent enough, or should be ignored in favor of an external repository.

There was a previous issue open about this (https://github.com/bazelbuild/bazel/issues/1813), but it had unclear motivations and was closed without action taken.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
jincommented, Jun 17, 2019

FWIW, rules_jvm_external has been using @erickj’s bazel-json parser in Starlark and we haven’t ran into any issues with it since the beginning of the project.

2reactions
jmillikin-stripecommented, Aug 26, 2019

There’s now a couple implementations of a JSON API in non-Bazel tools built on Starlark. To see if we can settle on a reasonable shared JSON API, I sent https://github.com/bazelbuild/starlark/pull/83 as a proposed standard JSON module in the Starlark spec.

Read more comments on GitHub >

github_iconTop Results From Across the Web

json - Bazel
a JSON object is parsed as a new unfrozen Starlark dict. Keys must be unique strings. a JSON array is parsed as new...
Read more >
Convert JSON to dictionary in Python - GeeksforGeeks
This function is used to parse the JSON string. Syntax: json.load(file_name) Parameter: It takes JSON file as the parameter. Return type: It ...
Read more >
JSON Files - Spark 3.3.1 Documentation
Property Name Default Scope primitivesAsString false read prefersDecimal false read allowComments false read
Read more >
Python Read JSON File – How to Load ... - freeCodeCamp
How JSON and Python Dictionaries work together in Python. How to work with the Python built-in json module. How to convert JSON strings...
Read more >
5 ways to process JSON data in Ansible - Opensource.com
The data for these models is exchanged as key/value pairs and encoded using different formats. JSON, which is widely used in Ansible, is...
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