Extension. JSON Decode. JsonPATH. Get the value easily. Convert XML <> JSON

Hello friends,

with this extension we can obtain the values ​​of a JSON text, writing its path.

We can also convert string XML to JSON and from JSON to XML.

JSON: https://www.json.org/json-es.html

In JSON we can find:

  • Object, the elements is in {brace}, for example: {"age": 25, "name": "Juan Antonio", "value": true}

  • Array, the elements is in [square bracket], for example: ["24", "Pedro Luis", false]

  • In Array the elements are numbered: store.book [1]

1.- JSON Examples: https://json-schema.org/learn/miscellaneous-examples.html

2.- JSON Examples: https://opensource.adobe.com/Spry/samples/data_region/JSONDataSetSample.html

3.- JSON validator: https://jsonformatter.curiousconcept.com/

4.- JSONPATH validator: https://jsonpath.com/

  • To convert XML <> JSON I have used this library:
  • To obtain the values ​​through the Json path I have used this library:
  • We can obtain the value of an element by writing its path:

$.store.bicycle.color

  • In this extension it is not necessary to put the $.

store.bicycle.color

  • To carry out the tests we will use the example of the author of the library:
{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby D ck",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}
6 Likes

1.- Decode JSON with JsonPath.

p299G_DecodificaJson.aia (402.5 KB)

com.KIO4_DecoJson.aix is here: App Inventor. Extensión. Decodifica Json. JsonPath. Convertir de Json a Xml y de Xml a Json.

- Blocks.

Set json text and a path. Gets a value or a list.

json_deco3

Validate and show (if possible) the Json string in easily readable form.

json_deco8

  • Convert JSON to XML and XML to JSON.

decodifica_json7

  • SetValue in a path.

json_deco13b

1.- Example app:

2.- Example with "Pretty" block.

p299G_DecodificaJson_pretty.aia (570.6 KB)


3.- Convert XML string to JSON string, and JSON string to XML string.

p299G_DecodificaJson_xml_json.aia (582.6 KB)

example.xml

<?xml version="1.0" encoding="UTF-8" ?>
 <rss version="2.0">
 <channel>
  <title>Título del RSS</title>
  <description>Descripción del RSS</description>
  <link>http://www.kio4.com/appinventor</link>
  <lastBuildDate>Mon, 06 Jan 2022 00:01:00 </lastBuildDate>
  <pubDate>Mon, 06 Jan 2022 16:20:00 </pubDate>
  <ttl>1800</ttl>
  
  <item>
   <title>Entrada dentro del RSS</title>
   <description>Descripción de la entrada</description>
   <link>http://www.kio4.com/raspberry</link>
   <guid>clave única</guid>
   <pubDate>Mon, 06 Jan 2022 17:20:00 </pubDate>
  </item>
  
 </channel>
 </rss>

decodifica_json6

4.- Set a value in a path.

  • In this example I establish the value "Juan Antonio" in the path .book[2].author

4 Likes

I wonder if it would make sense to bake the jsonpath logic into the get/set value at path blocks. Effectively, the list of inputs should be a rough blocks equivalent to the json path, so we could amend the block to take either the current list form or the more compact string form.

1 Like

I think it would be interesting to have a block between the App Inventor components to be able to get values ​​and lists from JSON easily.

2 Likes

@Juan_Antonio

This extension is epic :slight_smile:

I have been doing some work with firestore and the web component. firestore returns large json strings, and your extension makes easy the extraction of values. The syntax takes a little bit of getting used to (like anything new and shiney) but it is making light work of what would otherwise be json extraction agony!

Many thanks

thumbsup2

2 Likes

This extension has been updated on 2021/11/17.

Just now tried your extension. This makes life very easy to extract some Json. Thank you :+1:

1 Like

I saw there was a new version on 30 Jan 2022 - Released JsonPath 2.7.0. Could that be beneficial for your extension users? Couldn't find a changelog.

I have not seen improvement in the new version 2.7.0 for this extension. Also version 2.6.0 (which uses the extension) is 233k in size and 2.7.0 is 264k.

mmm may be useful for AJAX scraping

Possibly, but really nothing to do with scraping a rendered html page.....more about api calls and returns....

@Juan_Antonio thanks for this good working extension. The Json Path Java Implementation you used has also features for editing and setting values in a Json Object via its path. Do you think it would be possible to integrate that in your extension?

This extension only decodes Json, but does not edit.
For editing, maybe you can use the File component.

Yes I now that. But the linked Library what you used to decode Json can edit json is well, that's why I asked whether it is possible that you implement that in your extension?

I have updated the extension, now it contains the SetValue block

In path: .book[2].author
value: Juan Antonio

3 Likes

A post was split to a new topic: How do I use the "Get All TinyDB Extension" by @JuanAntonio