How to filter JSON objects through the fields of an array?

Asked

Viewed 251 times

0

How can I search for JSON objects by the tags assigned to them? Type I want to get all objects that have the "algodao" tag. I’m creating the model as it is down there for now, but I don’t know if it’s the best way, someone gives me a light?

{
  "ofertas": [
    {
      "id": 1,
      "categoria": "masculino",
      "titulo": "Blusa Clássica",
      "descricao_oferta": "Camisa confeccionada em tecido leve de algodão com poliéster.", 
      "anunciante": "riachuelo",
      "valor": 59.90,
      "destaque": true,
      "data" : "1970-01-01 00:00:00",
      "tags": [ 
        {"tag": "algodao"}, 
        {"tag": "blusa"}, 
        {"tag": "azul"}, 
        {"tag": "classica"} 
      ],
      "imagens": [
        {
          "url": "/assets/ofertas/camisa-social/camisa-classica-01.jpg"
        },
        {
          "url": "/assets/ofertas/camisa-social/camisa-classica-02.jpg"
        },
        {
          "url": "/assets/ofertas/camisa-social/camisa-classica-03.jpg"
        },
        {
          "url": "/assets/ofertas/camisa-social/camisa-classica-04.jpg"
        }
     ]
  },  
    ...
}

can be by browser locally, I just want to know the path I have to follow to get this object. And if the way I structured it is the most practical. I was trying it: http://localhost:3000/ofertas?tags?tag=algodao but I was unsuccessful.

  • in what language do you want to do this ? java , javascript php , python ?? :/

  • can be by browser locally, I just want to know the path I have to follow to get this object. And if the way I structured it is the most practical. I was trying it: http://localhost:3000/ofertas?tags?tag=algodao

  • ok if I’m not mistaken what you’re trying to do is not possible without an interface to interpret your json , json is nothing more than an encoding or JSON object is just a string that can be interpreted by programming languages, you will need something to retrieve your object (php ,js) would be ideal because you talked about recovering through a URL

1 answer

0


You can use some tool that offers reading through jsonpath expressions, as this site for example.

With your JSON, you can apply expressions like $.ofertas..imagens, that will select all nodes from imagens below ofertas:

[
  [
    {
      "url": "/assets/ofertas/camisa-social/camisa-classica-01.jpg"
    },
    {
      "url": "/assets/ofertas/camisa-social/camisa-classica-02.jpg"
    },
    {
      "url": "/assets/ofertas/camisa-social/camisa-classica-03.jpg"
    },
    {
      "url": "/assets/ofertas/camisa-social/camisa-classica-04.jpg"
    }
  ]
]

It is also possible to filter by array index, using $.ofertas..imagens[0]:

[
  {
    "url": "/assets/ofertas/camisa-social/camisa-classica-01.jpg"
  }
]

It is possible to make several filters on properties as well, but I recommend you to study on your own in the link I left at the beginning of the answer :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.